Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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 %. – w3dk Dec 20 '15 at 1:02
up vote 3 down vote accepted

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:

RewriteCond %{QUERY_STRING} ^data%5Bbranch%5D=xyz$
RewriteRule ^folder/site$ http://www.example2.com/test/? [R,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).

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 to test with 302/temp redirects first.)

share|improve this answer
    
@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
    
@Kevin welcome to WSE. You won't get a better answer than this, please select the answer as accepted to remove it from the unanswered list. – garth Dec 20 '15 at 23:18

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/
share|improve this answer
    
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.) – w3dk Dec 20 '15 at 1:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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