Forum Moderators: phranque
# Rewrite up to two instances of "%xx" to "?" & "=" in URL and do a 301-Moved Permanently redirect.
RewriteRule ([^\ ]+)\ ([^\ ]+)\ (.+) http://www.example.com/$1?$2=$3 [R=301,L] [edited by: jdMorgan at 1:28 pm (utc) on May 26, 2010]
[edit reason] example.com [/edit]
# If THE_REQUEST contains a URL with a percent-encoded "?" and/or a query string with
# one or more specific percent-encoded characters and we're not already fixing it,
# then copy the client-requested URL-plus-query-string into the "MyURI" variable.
RewriteCond %{ENV:MyURI} =""
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?\ ]*\?[^%\ ]*\%(25)*(3[Dd]|26)[^\ ]*)\ HTTP/ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(([^%\ ]*\%(25)*([^3].|.[^Ff]))*[^%\ ]*\%(25)*3[Ff][^\ ]*)\ HTTP/
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 first, 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?%6,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} [R=301,L]
[edited by: jdMorgan at 5:56 am (utc) on May 27, 2010]
# If THE_REQUEST contains a URL with a percent-encoded "?" and/or a query string with
# one or more specific percent-encoded characters and we're not already fixing it,
# then copy the client-requested URL-plus-query-string into the "MyURI" variable.
RewriteCond %{ENV:MyURI} =""
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?\ ]*\?[^%\ ]*\%(25)*(3[Dd]|26)[^\ ]*)\ HTTP/ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(([^%\ ]*\%(25)*([^3].|.[^Ff]))*[^%\ ]*\%(25)*3[Ff][^\ ]*)\ HTTP/
# OUT FOR TEST -- RewriteRule ^. - [NE,E=MyURI:%1]
RewriteRule ^. http://www.example.com [R=301,L]
# If an encoded "?" is present in the requested URI, and no unencoded "?" is
# present, then externally redirect to replace the encoded "?" character.
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?\ ]+)\ HTTP/
RewriteCond %1 ^(([^%]*(\%(25)*([^3].|.[^F]))*)*)\%(25)*3F(.*)$ [NC]
RewriteRule ^. http://www.example.com/%1?%7 [NE,R=301,L]
# 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]
RewtriteRule !^the-URL-I-want-to-fix [S=5]