【问题标题】:apache query string rewrite rulesapache查询字符串重写规则
【发布时间】:2016-11-01 14:01:14
【问题描述】:

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

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参数被添加到重定向页面

【问题讨论】:

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


    【解决方案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]
    

    【讨论】:

      【解决方案2】:

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

      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 上下文中收到查询字符串。

      【讨论】:

        猜你喜欢
        • 2016-08-13
        • 1970-01-01
        • 2016-12-08
        • 2018-02-08
        • 2015-09-10
        • 2011-04-18
        • 1970-01-01
        • 2013-03-19
        • 2012-03-08
        相关资源
        最近更新 更多