1

I have a URL that is:

http://example.com?country=uk&animal=dog&size=large

I wish to change the query string to

http://example.com?country=uk&animal=cat&size=small

I have tried this in an .htaccess tester

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{QUERY_STRING} ^(.*[&?]|)animal=(dog)&size=(large)([&?].*|)$
RewriteRule ^(.*)$ $1?%1animal=cat&size=small[R=301,NC,L]

But the & in the rewrite keeps getting shown a 686 !

http://example.com?country=uk&animal=cat686size=small

any ideas would be greatly appreciated.

2
  • Do you have more rules in this .htaccess? Can these parameters be in any order in query string? Commented Jun 24, 2021 at 18:04
  • No I have to keep them in that order as it is a stacked process. Commented Jun 24, 2021 at 18:06

1 Answer 1

1

With your shown samples, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.

##making engine one here.
RewriteEngine ON
##External redirect rule goes here. Please make sure to use correct back references to be used later on.
RewriteCond %{THE_REQUEST} \s/([^?]*)\?(country=)uk&(animal=)dog(&size=)large [NC]
##Applying redirection with 301 redirect here.
RewriteRule ^ /%1?%2uk&%3cat&%4small [R=301,L]
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @RavinderSingh13 i'm using htaccess.madewithlove.be to test the action, but it is saying that the `%{THE_REQUEST} is an unsuported string?
@Jay-UK, Please don't online sites to test your Rules, they don't work correctly, test it in your apache server and let me know then, cheers.
thanks for the advice there, so close now ! That makes the changes but insted of using the URL it inserts the entire file directory to the page, ie example.com/var/www/vhosts/example.com/…
@Jay-UK, sure, it's fixed now please do check it once and let me know if this helps you. Make sure to clear your browser cache before testing your URLs.
Hey Ravindaer that’s brilliant. Works a treat. Many thanks for your help.

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.