Search code examples
mod-rewriteurl-rewritingurlencodehtml-encode

Replace html encoding with url encoding


We are getting random hits like:

/abc?p=2&utm_campaign=xyz-campaign&utm_medium=email&utm_source=newsletter

Notice & which is html encoding for &. I have checked the possible sources but they all contain '&' only. I want replace all & with &. Is there a way to achieve it? I have removed complete string as of now using below given rule. But this is not right!

    RewriteCond %{QUERY_STRING} (&)
    RewriteRule (.*?) https://%{HTTP_HOST}%{REQUEST_URI}? [R=302,L]

Solution

  • Capture the referer in your logs so you can find the source of the problem and (also) fix it there.

    RewriteCond %{THE_REQUEST} ^\S++\s++([^\s?]*+\?\S*?&)amp;(\S*+)
    RewriteRule ^ %1%2 [DPI,L,R=301]
    

    That will do one redirect per ampersand without messing about decoding and re-encoding the URL. Less efficient but more reliable.