1

I use a PHP script with my site that produces a querystring in the URL. One of the variables in the querystring is: colour=red

I would like to make a rule in my .htaccess file that re-writes all instances of colour=red to colour=blue.

I don't have access to change the original PHP script so a .htaccess rule is needed.

1 Answer 1

4
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&|)colour=red(&.*|)$  # search colour=red
RewriteRule ^(.*)$ $1?%1colour=blue%2 [L,R=301]       # redirect to colour=blue

This will redirect URLs with colour=red in query-string to URLs with colour=blue

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

5 Comments

Hi, thanks for your reply. It didn't work, got an 'Internal Server Error' when I added your code to my .htaccess I'm afraid.
Is mod_rewrite available/enabled? In my case it worked as expected.
Hi, yes mod_rewrite is enabled on my server. I use it frequently to redirect certain pages and also to re-write. When adding your code to .htaccess an 'Internal Server Error' is encountered. Just in case, I commented out all other rules within my .htaccess file, but no joy I'm afraid.
The code above kind of only half solves my problem. Does anybody know how to actually re-direct the URL so that the querystring actually includes colour=blue rather than just $_GET ing it?
I edited my answer. Now it will actually replace colour=red with colour=blue and redirect to it.

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.