Search code examples
regex.htaccessurl-rewriting

.htaccess redirect error with query string change


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.


Solution

  • 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]