apache查询字符串重写规则

时间:2016-06-29 17:48:34

标签: apache mod-rewrite query-string url-redirection

我正在设置查询字符串重定向:

expo.com/en/general/campaigns/on-second-thought.html?slide=ost-2016-tank,以 expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html

RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} slide=ost-2016-tank
RewriteRule  ^/en/general/campaigns/on-second-thought.html?$  http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC] 

重定向正在发生但它的附加?slide = ost-2016-tank如下

http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html?slide=ost-2016-tank 

slide=ost-2016-tank参数已添加到重定向页面

2 个答案:

答案 0 :(得分:1)

由于您的规则未定义新的查询字符串,因此Apache的默认行为是将旧查询字符串复制到新URL。要删除它,请将?附加到您重写/重定向到的地址:

 RewriteRule  ^/en/general/campaigns/on-second-thought\.html?$  http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html? [R=301,L,NC]

或者,对于Apache> = 2.4,您还可以使用标志QSD(查询字符串丢弃):

RewriteRule  ^/en/general/campaigns/on-second-thought\.html?$  http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC,QSD]

答案 1 :(得分:1)

重定向时只需添加空白查询字符串:

RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} ^slide=(ost-2016-tank)$
RewriteRule  ^(/?en/general/campaigns/on-second-thought)\.(html)$ $1/%1.$2? [R=301,L,NC]

重定向时无需再次提及http://expo.com。由于R标志,它会自动重定向到相同的主机名。无需一遍又一遍地重复相同的字符串。使用匹配组并在以后引用它们。

您的模式中包含.html?$,这实际上意味着它将匹配.html以及.htm。您不会在RewriteRule上下文中收到查询字符串。