What I wish to do

I have one particular URL I need to redirect using .htaccess, based on a specific value being present in the query-string. A 301 permanent redirect header being sent to browser (that bit I know how to do).

The one URL to redirect is:


Where to redirect to

I want to redirect to a different domain: https://wa.mydomain.com.au/search-result/?location=western-australia.

Additional considerations

Note that there are other .../search-result/ URLs that have different variables in the query strings, such as ?weather=... or ?water=.... For example, https://www.mydomain.com.au/search-result/?location=western-australia&weather=part-shade&water=&pasture=. If any of those variables are in the query string, this doesn't impact whether or not it gets redirected. If location=western-australia is in the query-string, it should be redirected, along with its full query-string.

There are also other location values that may occur in the string (other than western-australia. These should not be redirected.

As seen in the above example, it's possible multiple variables will be in the query string, such as ?location=western-australia&weather=&water=moderate&pasture=.

So I need to test for the presence of location=western-australia irrespective of whether or not it has other variables after it. Note that the location= variable is always the first in the overall query string.

I think it will be as simple as testing for the presence of /search-result/ AND that it is followed by ?location=western-australia in the query-string. Or just testing for location=western-australia.

What I came up with so far

I tried shown below. Note: I tried two version of the RewriteCond and two versions of the RewriteRule. I've left both versions, but commented them out):

RewriteCond %{REQUEST_URI} ^/search-result/?$

# RewriteCond %{QUERY_STRING} (^|&)location=western-australia($|&)

RewriteCond %{QUERY_STRING} ^(location=western-australia)

RewriteRule ^$ https://wa.mydomain.com.au/search-result/?%{QUERY_STRING} [R=301,NC,L]

# RewriteRule ^$ https://wa.mydomain.com.au/search-result/?%1 [R=301,NC,L]

But it seems to have no effect.

Thank you.


1 Answer 1


So if you wish to redirect your URLs from subdomain www.example.com containing an exact query string to a new subdomain wa.example.com with the same query.

This htaccess will do the job:

RewriteEngine On 
RewriteCond %{QUERY_STRING} (^|&)location=western-australia
RewriteRule ^(.*)$ https://wa.example.com%{REQUEST_URI} [R=301,NC,L,QSA]
  • Thanks. I only want to redirect when the query string contains the variable and value I've mentioned in my question.
    – inspirednz
    Commented Apr 20, 2020 at 9:17
  • Sorry, I edited my Answer because I didn't understand well the first time
    – gael
    Commented Apr 20, 2020 at 11:04
  • Thanks. That worked. Would you mind taking a moment to explain why your RewriteRule worked, and why mine didn't? Am I correct in thinking ^(.*)$ refers to the entire original URI, which is then replaced by https://wa.example.com%{REQUEST_URI}?
    – inspirednz
    Commented Apr 20, 2020 at 23:15
  • yes it s a wildcard for anything corresponding to the RewriteCond to be rewritten
    – gael
    Commented Apr 21, 2020 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.