4

我有许多由 Google 网站管理员工具报告的入站链接,其中包含导致 404 错误的实体。我想创建一些与 IIS 一起使用的 url 重写规则来解决这个问题。这些规则需要在任何页面上起作用,因为某些包含实体的链接可能是多个文件夹,并且是 index.php 以外的文件。

例如重写规则应该能够处理:

www.mysite.com/index.php%3Fpage%3Dtest

www.mysite.com/folder/somepage.php%3Fvariable%3Dtest

并将它们转换为:

www.mysite.com/index.php?page=test

www.mysite.com/folder/somepage.php?variable=test

我尝试创建自己的规则,但并没有走得太远。我找到了处理此问题的 Apache 规则,但使用 IIS 导入规则工具导入它们不起作用。

以下是适用于 Apache 的方法(现在我们只需要一个 IIS 版本):

# If THE_REQUEST contains a URL-path with a percent-encoded "?" and/or a query string with one
# or more specific percent-encoded characters, and we're not already in the process of fixing
# it, then copy the client-requested URL-path-plus-query-string into the "MyURI" variable.
RewriteCond %{ENV:MyURI}>%{THE_REQUEST} ^>[A-Z]+\ /([^\ ]+)\ HTTP/
RewriteCond %1 ^([^?]*\?([^%]*(\%(25)*([^3].|.[^D]))*)*\%(25)*3D.*)$ [NC,OR]
RewriteCond %1 ^([^?]*\?([^%]*(\%(25)*([^2].|.[^6]))*)*\%(25)*26.*)$ [OR]
RewriteCond %1 ^(([^%]*(\%(25)*([^3].|.[^F]))*)*\%(25)*3F.*)$ [NC]
RewriteRule ^. - [NE,E=MyURI:%1]
#
# If any encoded question mark is present in the client-requested URI, and
# no unencoded question mark is present, replace the first encoded question
# mark, queue up a redirect, and then re-start mod_rewrite processing
RewriteCond %{ENV:MyURI} ^[^?]+$
RewriteCond %{ENV:MyURI} ^(([^%]*(\%(25)*([^3].|.[^F]))*)*)\%(25)*3F(.*)$ [NC]
RewriteRule ^. - [NE,E=MyURI:%1?%7,E=QRedir:Yes,N]
#
# If any encoded "=" sign follows the "?", replace it, queue
# up a redirect, and re-start mod_rewrite processing
RewriteCond %{ENV:MyURI} ^([^?]*\?([^%]*(\%(25)*([^3].|.[^D]))*)*)\%(25)*3D(.*)$ [NC]
RewriteRule ^. - [NE,E=MyURI:%1=%7,E=QRedir:Yes,N]
#
# If any encoded ampersand follows the "?", replace it, queue
# up a redirect, and then re-start mod_rewrite processing
RewriteCond %{ENV:MyURI} ^([^?]*\?([^%]*(\%(25)*([^2].|.[^6]))*)*)\%(25)*26(.*)$
RewriteRule ^. - [NE,E=MyURI:%1&%7,E=QRedir:Yes,N]
#
# If we get here, there are no more percent-encoded characters which can
# and should be replaced by the rules above, so do the external redirect
RewriteCond %{ENV:QRedir} =Yes [NC]
RewriteRule ^. http://www.example.com/%{ENV:MyURI} [NE,R=301,L]

这至少让我们了解了在编写 IIS url 重写规则时需要考虑什么。

4

1 回答 1

1

此规则适用于您:

<rule name="3f and 3d redirect">
    <match url="(.*)(\?|\=)(.*)" />
    <action type="Redirect" url="{R:0}" />
</rule>
于 2017-07-29T17:36:05.240 回答