Reputation: 41
I have a number of inbound links reported by Google Webmaster Tools that contain entities which result in 404 errors. I would like to create some url rewrite rules that will work with IIS to take care of this. These rules need to work on any page since some of the links that contain entities could be several folders deep and be a file other that index.php.
For example the rewrite rules should be able to handle :
www.mysite.com/index.php%3Fpage%3Dtest
www.mysite.com/folder/somepage.php%3Fvariable%3Dtest
and convert them into:
www.mysite.com/index.php?page=test
www.mysite.com/folder/somepage.php?variable=test
I tried creating my own rules and didn't get very far. I found rules for Apache that handle this but importing them by using the IIS import rules tool didn't work.
Here is what works for Apache (now we just need an IIS version):
# 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]
This at least gives us an idea of what needs to be thought about when writing the rules for IIS url rewrite.
Upvotes: 4
Views: 1474
Reputation: 8736
This rule will work for you:
<rule name="3f and 3d redirect">
<match url="(.*)(\?|\=)(.*)" />
<action type="Redirect" url="{R:0}" />
</rule>
Upvotes: 2