3

On a website I'm working on now I need to set up a redirection that takes a query string into consideration.

Example:

http://www.domain.com/?id=86&name=John&ref=12d34 -> http://www.domain.com/?ref=12d34
http://www.domain.com/?ref=593x56&id=935 -> http://www.domain.com/?ref=593x56
http://www.domain.com/?ref=3v77l32 -> http://www.domain.com/?ref=3v77l32

So basically I need to find the ref parameter and it's value (whatever it's length) and append only that part to the new URL. The issue is the the ref parameter can appear anywhere within the URL.

Any help or guidance would be very much appreciated!

0

2 Answers 2

5

RewriteRule does not work with query string directly -- you have to use RewriteCond for that.

Here is the rule -- it will redirect (301 Permanent Redirect) ANY URL that has more than 1 parameter in query string and 1 of them is ref

RewriteEngine On

RewriteCond %{QUERY_STRING} (^|&)ref=([^&]*)(&|$)
RewriteCond %{QUERY_STRING} !^ref=([^&]*)$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}?ref=%2 [R=301,L]

For example:

  1. It will redirect http://www.example.com/hello.php?id=86&name=John&ref=12d34888&me=yes to the same URL but with ref parameter only: http://www.example.com/hello.php?ref=12d34888.

  2. It will do nothing if only ref parameter is present or no parameter at all, e.g. http://www.example.com/hello.php?ref=12d34888 or http://www.example.com/hello.php.


If such redirect should only work for website root hits, then change the RewriteRule line to this:

RewriteRule ^$ http://%{HTTP_HOST}/?ref=%2 [R=301,L]

(this is if placed in .htaccess file in website root folder -- if placed in server config / virtual host context the rule needs to be slightly tweaked).

http://www.example.com/?id=86&name=John&ref=12d34888&me=yes -> http://www.example.com/?ref=12d34888


If it has to be redirected to another domain, then replace %{HTTP_HOST} by domain specific name, e.g:

RewriteRule ^$ http://www.exampe.com/?ref=%2 [R=301,L]

It all has been tested before posting.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow! Thank you so much for the detailed explanation. I'll comment again once I've tested it.
It works just as I needed it to. Thank you LazyOne Kenobi. You were my only hope.
-1
RewriteEngine On
RewriteRule ref=(.*?) /?ref=$1 [L]

is about as simple as it guess, but this will screw up if the ref query var isn't the last thing in the line - it'll slurp up everything after ref= and redirect that to, so

/blah&ref=abc&blahblahblah

would turn into

/ref=abc&blahblahblah

5 Comments

Thank you much Marc! I may indeed use this despite the caveat. Would you perhaps be able to point me in the right direction should I want to obtain the exact value instead of everything after the ref parameter?
Much obliged, I'll give that a shot.
RewriteRule does not work with query string parameters directly
Thanks LazyOne, what would you suggest I use instead?
@Tom See my answer. @Mark The pattern you provided is not safe, as it will also work if parameter's name has ref at the end, for example myref: ?myref=12345&id=56789 -- there is no ref parameter here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.