My client wants a query string munged (by changing % to A) on certain pages.
For example, I can remove the query string completely on the desired pages via:
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1? [R=301,L] #remove query string
Here’s what I thought should remove % on the query string and replace with A but it’s not:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
What am I doing wrong in this? I just can’t quite spot it. Thanks for the expert eyes!
You’re real close.
The problem here is that you’ve got a condition and the match of your rule should be together. Your backreference to the previous
RewriteCondis broken because it’s for the REQUEST_URI and not the QUERY_STRING like you want.Here, the %1 backreference matches the
(.*)at the end of the/SpecialPageURI. The backreferences from your query string match gets lost, and that’s the ones you really want. You can combine the condition to match the REQUEST_URI with the regular expression pattern in theRewriteRule:Here, the %1 and %2 backreferences correctly reference the query string and the
SpecialPagecondition in the URI is met by the regex pattern.