301重定向:匹配查询字符串,不要将其附加到新的重定向

时间:2013-04-23 06:37:45

标签: .htaccess redirect

我在.htaccess文件中设置了多个301重定向,但是我遇到了一个查询字符串问题,导致重定向不匹配。

示例:

Redirect 301 /about/history/?lang=fr http://www.newdomain.com/fr/history
Redirect 301 /about/history/ http://www.newdomain.com/nl/history

因此olddomain.com/about/history/?lang=fr现在与第二条规则匹配,并重定向到http://www.newdomain.com/nl/history?lang=fr

我希望它从字面上取?lang=fr而不是将查询字符串附加到新的重定向。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

Redirect采用URL- 路径,其中不包含查询字符串。因此,第一个Redirect永远不匹配。

要实现您的目标,您可以尝试某种content negotiation或使用mod_rewrite

RewriteEngine on
RewriteCond %{QUERY_STRING} lang=fr
RewriteRule /about/history/ http://www.newdomain.com/fr/history? [R,L]
RewriteRule /about/history/ http://www.newdomain.com/nl/history [R,L]

如果一切正常,您可以将R更改为R=301