【问题标题】:.htaccess redirect based on query string and remove query string.htaccess 基于查询字符串重定向并删除查询字符串
【发布时间】:2011-08-24 12:28:04
【问题描述】:

我需要重定向指令来将带有查询字符串的旧 URL 重定向到没有查询字符串的新 URL。新的 URL 取决于查询字符串的值。例如:

我想要: http://www.mydomain.com/product.php?id_product=AAA

将重定向到:

http://www.mydomain.com/product-name-for-product-number-1

http://www.mydomain.com/product.php?id_product=BBB

将重定向到:

http://www.mydomain.com/this-is-different-product-name-for-product-number-2

提前致谢。

【问题讨论】:

    标签: string .htaccess redirect


    【解决方案1】:

    你可以做这种类型的重定向:

    来自:http://www.mydomain.com/product-name-for-product-number-AAA
    至:http://www.mydomain.com/product.php?id_product=AAA&name_product=product-name

    来自:http://www.mydomain.com/this-is-different-product-name-for-product-number-2
    至:http://www.mydomain.com/product.php?id_product=BBB&name_product=this-is-different-product-name

    您无法从 htaccess 文件中动态检索产品名称,唯一的方法是:

    RewriteRule ^(.+)-for-product-number-(.+)$ ^product.php?id_product=$2&name_product=$1
    

    或者您需要为您拥有的每个产品创建一个重写器(您可以从从数据库中检索所有产品 ID 和名称的脚本生成 .htaccess):

    RewriteRule ^product-name-for-product-number-1$ product.php?id_product=AAA
    RewriteRule ^this-is-different-product-name-for-product-number-2$ product.php?id_product=BBB
    

    【讨论】:

      【解决方案2】:

      在我看来,您无法根据查询字符串执行重定向。因此,您必须添加 [QSA] 标志并重定向到一个脚本,该脚本将能够通过查询字符串进行重定向。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-30
        • 2014-02-02
        • 1970-01-01
        • 1970-01-01
        • 2015-03-31
        • 1970-01-01
        相关资源
        最近更新 更多