Search code examples
.htaccesshttp-status-code-301

Do 301 redirects work on urls that contain query strings?


I've searched for this but the answers I've found seem to be around rewriting URLs or for redirecting lots of pages in an entire section and look confusing (lots of regex).

We need to add some 301 redirects and everything is working fine except for 13 URLs that contain query strings.

Is there a trick to redirecting these kinds of urls?

Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=39 /blogsearch/Coffee
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=26 /blogsearch/Food
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=27 /blogsearch/Wine
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=29 /blogsearch/Travel
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=37 /blogsearch/Hotel
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=49 /blogsearch/Apartments

Any help would be much appreciated.

Cheers

Ben


Solution

  • Yes there's a trick. You can't use Redirect directive with query strings.

    You could just do something like this with mod-rewrite. Just change the corresponding query string value for every individual link you have.

    RewriteEngine On
    RewriteCond %{QUERY_STRING} (.+)=39
    RewriteRule ^blogsearch/?$ /blogsearch/Coffee? [L,NC,R=301]
    RewriteCond %{QUERY_STRING} (.+)=26
    RewriteRule ^blogsearch/?$ /blogsearch/Food? [L,NC,R=301]
    RewriteCond %{QUERY_STRING} (.+)=27
    RewriteRule ^blogsearch/?$ /blogsearch/Wine? [L,NC,R=301]
    RewriteCond %{QUERY_STRING} (.+)=29
    RewriteRule ^blogsearch/?$ /blogsearch/Travel? [L,NC,R=301]
    RewriteCond %{QUERY_STRING} (.+)=37
    RewriteRule ^blogsearch/?$ /blogsearch/Hotel? [L,NC,R=301]
    RewriteCond %{QUERY_STRING} (.+)=49
    RewriteRule ^blogsearch/?$ /blogsearch/Apartments? [L,NC,R=301]