大网数据广告活动
网站运营与故障问答

.htaccess to redirect URL with specific query-string, to a different domain, maintaining the query-string

· 52CE 我爱测

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 …

问题原文

原帖链接 · 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]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2020-04-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。