【问题标题】:Redirect the URL from one query string to another将 URL 从一个查询字符串重定向到另一个
【发布时间】:2021-11-29 19:14:56
【问题描述】:

我花了很多时间试图找到解决方案,并尝试了许多不同的方法,但到目前为止我所尝试的都没有奏效。

我想将带有查询字符串的 URL 重定向到另一个包含该查询字符串值的 URL。

我要重定向:

https://example.com/component/search/?searchword=XXXXXXXXX&searchwordsugg=&option=com_search

https://example.com/advanced-search?search=XXXXXXXXX

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    您可以在根 .htaccess 文件的顶部使用 mod_rewrite 执行以下操作:

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} (?:^|&)searchword=([^&]*)
    RewriteRule ^component/search/?$ /advanced-search?search=%1 [NE,R=302,L]
    

    RewriteRule pattern 仅与 URL 路径匹配,特别是不包括查询字符串。为了匹配查询字符串,我们需要一个单独的 condition 来检查 QUERY_STRING 服务器变量。

    %1 是对前面 CondPattern 中第一个 capturing 组的反向引用,即。 searchworld URL 参数的

    正则表达式(?:^|&)searchword=([^&]*) 匹配查询字符串中的searchworld URL 参数anywhere,而不仅仅是在开头(如您的示例中)。这也允许 URL 参数的 值。

    NE 标志 是必需的,以防止捕获的 URL 参数值在响应中被双重编码。 (因为QUERY_STRING 服务器变量未进行 % 解码。)

    L 标志防止在重写引擎的此通过期间进一步处理。

    参考:

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 2013-01-10
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多