2

I am trying to do a rewrite of the site from

http://www.example.com/folder/site?data%5Bbranch%5D=xyz

to

http://www.example2.com/test/

Already tried the query string but facing issues with %. Any help?

  • 1
    "Already tried the query string" - I'm curious, how were you doing it before? There's nothing special about the % in the query string; it is matched as a literal %. – MrWhite Dec 20 '15 at 1:02
5

Note that if you need to match against the query string, you need to compare against the QUERY_STRING server variable in a mod_rewrite RewriteCond directive. You can't match against the query string using a mod_alias Redirect (or RedirectMatch) or the RewriteRule (mod_rewrite) pattern - these all match against the URL-path only, which notably excludes the query string.

Otherwise, there is nothing special about the % in the query string, it is matched as a literal % in the QUERY_STRING server variable (which is not decoded). Try the following:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^data%5Bbranch%5D=xyz$
RewriteRule ^folder/site$ http://www.example2.com/test/? [R=302,L]

It is more efficient to match the URL-path in the RewriteRule pattern, rather than using a condition to match against REQUEST_URI. The RewriteRule pattern is first matched, and only if this is successful are the conditions (above it) processed.

No need to wrap the query string value in double quotes. But if you want to match just that query string, you will need start/end anchors (ie. ^...$). (Or use the exact match - lexicographically equal - operator prefix, =, ie. =data%5Bbranch%5D=xyz. Everything after the = prefix is treated as a literal string, not a regex).

The ? is required on the end of the RewriteRule substitution in order to remove the query string from the request, otherwise it will be passed through to the substituted URL. By ending the URL with ? essentially creates an empty query string (the ? itself is not included in the resulting URL). (If you are on Apache 2.4+ then you can use the QSD - Query String Discard - flag instead.)

The L flag ensures no other rules are processed if this is successful. Which is usually what you want to do in the case of an external redirect.

If this is to be a permanent redirect then change the R=302 (temporary) flag to R=301 once it is working OK. If you don't explicitly use the R flag to force an external redirect, it will be implicitly redirected because you have specified an absolute URL in the substitution. It is always better to be explicit. (NB: 301/permanent redirects are cached by the browser so it is often easier/safer to test with 302/temp redirects first. Or test with browser caching disabled.)

  • @Kevin - w3d is the best we have. You are in good hands!! – closetnoc Dec 20 '15 at 2:11
  • @closetnoc yes I can see that! :) – Kevin Dec 20 '15 at 11:41
  • @w3d this is a much better answer than mine! Learned some new things as well! Thank you! – Kevin Dec 20 '15 at 11:42
  • But trying to match the request uri in the rewriterule doesn't work. that's why we use the request_uri condition match. Apache is weird like that. – ahnbizcad Sep 14 '16 at 1:06
  • @ahnbizcad That doesn't really make sense - there is no obvious reason why you can't match the URL-path with the RewriteRule pattern - in fact, that would be preferable (as mentioned in my answer). Note, however, that the URL-path matched by the RewriteRule pattern excludes the directory-prefix in per-directory rewrites. Whereas the REQUEST_URI server variable includes the full URL-path from the request - so the regex to match these are not interchangeable. Also note, with respect to this question, that the URL-path notably excludes the query string. – MrWhite Sep 14 '16 at 11:02
1

Seems like I found an answer to my own question. Guess it is the right way to do it, but anyway:

RewriteCond %{REQUEST_URI}  ^/folder/site$
RewriteCond %{QUERY_STRING} "data%5Bbranch%5D=xyz"
RewriteRule (.*) http://www.example2.com/test/
  • There's a few issues with the code you've posted, I've added an answer with more explanation. (You also don't need to capture the matched URL if you aren't using it in the substitution. ie. the parentheses around (.*) are unnecessary.) – MrWhite Dec 20 '15 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.