404 エラーになるエンティティを含むインバウンドリンクが Google ウェブマスター ツールによって報告されています。これを処理するために、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 書き換えのルールを作成する際に何を考慮する必要があるかがわかります。