【问题标题】:Using htaccess to redirect query urls使用 htaccess 重定向查询 url
【发布时间】:2013-10-24 17:55:37
【问题描述】:

我需要使用 htaccess 重定向几个不同类别中的多个 url。目前的结构是;

.com/category/subcat/product.html?querystring=123

而且我需要重定向到所有版本的查询字符串 URL;

.com/product.html

我不介意为多个类别写几行,但查询字符串有 1000 种变体,无法将它们全部写出来。

有人有什么想法吗??

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    尝试将此添加到文档根目录中的 htaccess 文件中:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} (^|&)querystring=123(&|$)
    RewriteRule ^category/subcat/product\.html$ /product.html? [L,R=301]
    

    至于变化,如果只是数字ID不同的问题,那么您可以使用:

    RewriteCond %{QUERY_STRING} (^|&)jewelry_(metal|carat|colour|clarity)=[0-9]+(&|$)
    

    如果“产品”部分应该是可变的,则将带有规则的行替换为:

    RewriteRule ^category/subcat/(.+)\.html$ /$1.html? [L,R=301]
    

    如果 "category" 和 "subcat" 可以是任何东西,那么:

    RewriteRule ^[^/]+/[^/]+/(.+)\.html$ /$1.html? [L,R=301]
    

    【讨论】:

    • 感谢您的回复,查询字符串不仅仅是整个字符串可以更改的数值,我需要适应这一点。我需要保持 /product.html 相同,但想将此 htaccess 用于所有产品,例如 /product1.html 和 /product2.html 等。
    • @user2267784 你是说查询字符串可以是任何东西吗?如果有查询字符串,重定向?
    • 是的,因此产品上的当前查询字符串是 ?jewelry_metal=324&jewelry_carat=2&jewelry_colour=276&jewelry_clarity=261 但这可能会更改顺序、数字并且并非所有查询都会显示。所以我需要 /products23.html?jewelry_metal=324&jewelry_carat=2&jewelry_colour=276&jewelry_clarity=261 和变体重定向到 /products23.html
    • @user2267784 所有这些东西从一开始就会非常有用,因为没有这些信息就不可能回答你的问题。请参阅上面的编辑
    • 很抱歉之前没有详细说明此信息,感谢您提供的更新信息
    猜你喜欢
    • 2014-08-24
    • 2021-04-05
    • 1970-01-01
    • 2018-08-18
    • 2022-03-24
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多