Search code examples
apachehttp-status-code-301

301 Redirect with query string and domain name in Apache


I'm trying to write a 301 redirect that will look at both the host name and the query string parameter. So if the URL is either of the two.

Then I want it to redirect to the appropriate page:

Otherwise, if host is not example.com like

Then I do not want it to redirect.

This is what I have so far, but it doesn't seem to work. If anyone could provide a bit of help on this one, it would be most appreciated. I already tried to find a similar answer but I couldn't find one.

RewriteEngine on

RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteCond %{query_string} p=11&b=3
RewriteRule (.*) http://store.example.com/testpage.html [R=301,L]

Solution

  • You need to specify an empty query for your substitution URL:

    RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
    RewriteCond %{QUERY_STRING} p=11&b=3
    RewriteRule .* http://store.example.com/testpage.html? [R=301,L]
    

    Otherwise the original requested query gets automatically appended to the new URL.