【问题标题】:Apache Mod_Rewrite query string to standard urlApache Mod_Rewrite 查询字符串到标准 url
【发布时间】:2010-03-07 18:07:41
【问题描述】:

我使用 .htaccess 重定向 301 重定向一个 URL,然后将所有查询字符串元素附加到末尾,留下一些在 Google 中索引为 /store/product/?d=department&s=section&p=product 的 URL。

我已经通过使用RewriteRule 修复了重定向,而不是附加查询字符串,但是我被困在尝试将带有查询字符串的旧重定向 URL 重写回原始 URL(因为这些正在寻找现在就像两个不同的 Google 网址)。

我已经设法让RewriteRule 进行某种工作,因为/store/product/xxxxx 应该重定向到/store/product/,但它似乎不适用于整个查询字符串。

我一直用的是:

RewriteRule ^store/product/([a-zA-Z0-9\-_\?=&]+)$ http://www.example.com/store/product/ [NC,R=301,L]

RewriteRule ^store/product/\?d=department&s=section&p=product$ http://www.example.com/store/product/ [NC,R=301,L]

希望一切都有意义!

非常感谢

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:
    RewriteCond %{QUERY_STRING} !^$
    RewriteRule ^store/product/.*$ http://www.example.com/store/product/? [NC,R=301,L]
    

    【讨论】:

    • 您好,这似乎给了我一个重写循环,有没有办法只在查询字符串中有明确的内容时才进行重写?
    【解决方案2】:

    您需要在替换中指定一个空查询,以免原始查询自动附加到新 URL:

    RewriteRule ^store/product/[a-zA-Z0-9\-_=&]+$ http://www.example.com/store/product/? [NC,R=301,L]
    

    注意替换 URL 末尾的 ?

    【讨论】:

      猜你喜欢
      • 2014-07-27
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2012-08-30
      • 2015-06-12
      • 2011-03-28
      相关资源
      最近更新 更多