【问题标题】:htaccess rewrite url with and without query stringhtaccess 使用和不使用查询字符串重写 url
【发布时间】:2018-08-01 18:16:27
【问题描述】:

我有一个重写规则,在访问没有参数的直接 URL 时有效。

示例网址:http://mev-hongkong.com/product-category/babies-tots/

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mev-hongkong\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^product-category/babies-tots(/.*)?$ https://mothersenvogue.com.hk/shop/all-babies-tots$1 [L,R=301,NC,NE]

但是,我也有相同的 URL,它们有一个参数,我还需要重定向到相同的 URL 输出。我尝试操纵重写规则并添加了一个%{QUERY_STRING} 条件,如下面的规则,但是当我使用具有参数的 URL 对其进行测试时,查询字符串仍然没有被删除。

带参数的示例网址:http://mev-hongkong.com/product-category/babies-tots/?add_to_wishlist=8836

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mev-hongkong\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^add_to_wishlist=([^&]+) [NC]
RewriteRule ^product-category/babies-tots(/.*)?$ https://mothersenvogue.com.hk/shop/all-babies-tots$1 [L,R=301,NC,NE]

为了正确剥离查询字符串,我错过了什么?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    QSD 标志正是这样做的:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www\.)?mev-hongkong\.com$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule ^product-category/babies-tots(/.*)?$ https://mothersenvogue.com.hk/shop/all-babies-tots$1 [L,R=301,NC,NE,QSD]
    

    【讨论】:

    • 非常感谢@Anonymous!这完全解决了它!
    • @Weisen 太好了。很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 2012-11-15
    • 2016-02-13
    • 2020-06-04
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    相关资源
    最近更新 更多