.htaccess仅当GET参数存在时才重定向

编程入门 行业动态 更新时间:2024-10-05 07:17:51
本文介绍了.htaccess仅当GET参数存在时才重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的一个客户的网站很旧,没有漂亮的 URL。因此,目前看起来像这样:

I have a client with an old website without 'pretty' URLs. So currently it looks like this:

www.domain/?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/?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/?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 # to sub.domain no mather what RewriteCond %{HTTP_HOST} ^www\.domain\$ [NC] RewriteRule ^(.*)$ sub.domain/$1 [R=301,L] # But this DOESN'T work RewriteRule ^/?w(.*) sub.domain/?w$1 [R=301,L] # Also tried to redirect 'by hand', but DIDN'T work either Redirect 301 /?w=42 sub.domain/?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 ^(.*)$ sub.domain/$1 [L,R=301]

请注意,查询字符串会自动附加到规则目标的末尾。

Note that the query string gets automatically appended to the end of the rule's destination.

更多推荐

.htaccess仅当GET参数存在时才重定向

本文发布于:2023-11-02 06:31:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时才   重定向   参数   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!