【问题标题】:Redirect with dynamic parameters .htaccess使用动态参数重定向 .htaccess
【发布时间】:2022-01-09 20:31:51
【问题描述】:

urlss=changingtext中有一个动态参数。需要识别这个参数,并为其添加另一个参数,以便重定向得到s=changingtext&post_type=product

有一个链接例如

example.com/?min_price=0&max_price=0&s=test

它应该总是重定向到

example.com/?min_price=0&max_price=0&s=test&post_type=product

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    提供 URL 参数的顺序并不重要,然后您可以使用 .htaccess 文件顶部的 mod_rewrite 来执行此操作:

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} (^|&)s=[^&]+
    RewriteRule ^ %{REQUEST_URI}?post_type=product [QSA,R=302,L]
    

    这将首先添加post_type URL 参数,并使用QSA(查询字符串附加)标志附加现有的查询字符串。 IE。在您的示例中,它将被重定向到example.com/?post_type=product&min_price=0&max_price=0&s=test

    这专门检查非空的s URL 参数。一个空的 URL 参数,例如。 foo=1&s=&bar=1 不会被重定向。

    这适用于任何 URL 路径。

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2013-08-29
      • 2015-10-01
      • 2015-03-28
      • 2021-06-30
      相关资源
      最近更新 更多