问题原文
原帖链接 · inspirednz · CC BY-SA 4.0
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:
https://www.mydomain.com.au/search-result/?location=western-australia.
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.
已采纳解答
原帖链接 · gael · CC BY-SA 4.0
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]
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
