53

We have some very basic mod_rewrite rules:

RewriteRule ^something.swf$ http://www.newdomain.com/something.swf [R=302,L]

mod_rewrite seems to do its job, and if the user is accessing:

something.swf?param=value, it is redirected correctly to http://www.newdomain.com/something.swf?param=value

the problem is in this situation:

www.olddomain.com/something.swf?param=URL_ENCODED_VALUE

what ends up happening is mod_rewrite takes it upon it self to re-urlencode the query string param

so what the user ends up with is:

www.olddomain.com/something.swf?param=URL_ENCODED_VALUE

REDIRECTED TO

www.newdomain.com/something.swf?param=URL_ENCODED_VALUE_OF_URL_ENCODED_VALUE

so we end up with a double-urlencoded value. boourns!

While I do understand we could make a \?(.*) ... ?$1 rule for this, I am thinking there must be a way to tell mod_rewrite NOT to urlencode the query string params... we would like to avoid using 2 rules since valid paths are:

something.swf (no query string)

and

something.swf?someparams...

so yea, ideally... just tell mod_rewrite: please, no urlencoding of query string params... just direct passthru to the new URL via R=302.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
anonymous-one
  • 14,454
  • 18
  • 60
  • 84
  • 2
    doh! the answer is, the NE parameter. sorry! – anonymous-one Jun 29 '11 at 12:36
  • 2
    Is bet it was. Can you provide an answer and mark it as accepted? (I guess there is a time limit to do that, but do it as soon as you can please :P) – M'vy Jun 29 '11 at 12:59
  • yea says i need 100 rep to answer my own question in less then 8 hours. will answer later. – anonymous-one Jun 29 '11 at 13:26
  • possible duplicate of [Apache2 redirect with query string escaped twice](http://stackoverflow.com/questions/6268257/apache2-redirect-with-query-string-escaped-twice) – regilero Jun 29 '11 at 21:53

1 Answers1

103

the way to accomplish this is via the NE (no escape) paramater.

RewriteRule ^something.swf$ http://www.newdomain.com/something.swf [R=302,L]

should in fact read

RewriteRule ^something.swf$ http://www.newdomain.com/something.swf [R=302,NE,L]

this will force mod_rewrite to leave all query string values as they are, without doing any encoding / escaping.

as easy as that :)

anonymous-one
  • 14,454
  • 18
  • 60
  • 84
  • 1
    There may actually be a bug associated with this. NE is a good option until you have URL encoded characters in the path segment of the URL as well. See: http://serverfault.com/questions/331899/apache-mod-rewrite-double-encodes-query-string-on-redirect – Ryan Nov 14 '14 at 17:01
  • 1
    Dropping the flag's documentation link here for future readers: https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_ne – Sankalp Jul 11 '20 at 09:38