.htaccess仅当GET参数存在时才重定向 [英] .htaccess redirect only if GET parameter exists
问题描述
我的一个客户的网站很旧,没有漂亮的 URL。因此,目前看起来像这样:
I have a client with an old website without 'pretty' URLs. So currently it looks like this:
http://www.domain.com/?w=42&a=5&b=3
参数值仅是数字。
现在,他们希望将旧站点移至子域,而主(www)域将成为新站点(具有SEO友好URL的WP)的所在地。
Now they want to move the old site to a subdomain and main (www) domain would be home to a new website (WP with SEO friendly URLs).
现在我想做的是将所有来自 /?w =< num>
(并且只有那些)到 sub.domain.com/?w=<num<
,这样,现有链接(大部分来自Google)就会重定向到子域页面,
Now what I would like to do is redirect all requests that come to the /?w=<num>
(and ONLY those) to sub.domain.com/?w=<num>
, so that existing links (mostly from Google) get redirected to the subdomain page, while the new page works serving new content thorough pretty URLs.
我尝试了以下操作:
# This works, but redirects the entire www.domain.com
# to sub.domain.com no mather what
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
# But this DOESN'T work
RewriteRule ^/?w(.*) http://sub.domain.com/?w$1 [R=301,L]
# Also tried to redirect 'by hand', but DIDN'T work either
Redirect 301 /?w=42 http://sub.domain.com/?w=42
我在做什么错?我搜寻了很多东西,但总是得到这样的建议。或者也许我只是在搜索错误的关键字...
What am I doing wrong? I searched high and low but always end up with this kind of suggestions. Or maybe I'm just searching for wrong keywords ...
谢谢!
推荐答案
您无法与重写规则或重定向指令中的查询字符串匹配。您需要与%{QUERY_STRING}
变量匹配。尝试:
You can't match against the query string inside a rewrite rule or a redirect directive. You need to match against the %{QUERY_STRING}
variable. Try:
RewriteCond %{QUERY_STRING} (^|&)w=[0-9]+(&|$)
RewriteRule ^(.*)$ http://sub.domain.com/$1 [L,R=301]
请注意,查询字符串会自动附加到规则目标的末尾。
Note that the query string gets automatically appended to the end of the rule's destination.
这篇关于.htaccess仅当GET参数存在时才重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!