【问题标题】:Rewriting query string to another query string将查询字符串重写为另一个查询字符串
【发布时间】:2015-08-05 03:59:31
【问题描述】:

我需要像这样获取传递到我的网站的搜索网址:

 /index.php?keyword=47174&Search=Search&Itemid=1&option=com_virtuemart&page=shop.browse

并将它们更改为:

 /catalogsearch/result/?q=47174

我需要在 "keyword=" 之后取值忽略 & 符号之后的所有内容,并将其提供给 ?q= 之后的第二个 url

这是我目前想出的:

 RewriteCond %{QUERY_STRING} ^keyword=([a-z][0-9a-z_]+)$
RewriteRule ^index\.php$ /catalogsearch/result/ [L]

但是,这也会在 url 的末尾打印关键字=,不会打印 q= 或清除 & 之后的所有内容

我该如何解决这个问题?

【问题讨论】:

    标签: regex .htaccess url mod-rewrite rewrite


    【解决方案1】:

    您可以使用以下内容:

    RewriteRule ^index\.php$ /catalogsearch/result/?q=$1 [L]
                                                   ^^^^^
    

    其中$1 是对捕获的组 1 的反向引用(这里是关键字的值)

    【讨论】:

      【解决方案2】:

      试试这个:

       RewriteCond %{QUERY_STRING} ^keyword=([^&]+) [NC]
      RewriteRule ^index\.php$ /catalogsearch/result/?q=%1 [NC,L,R]
      

      【讨论】:

      • 这正是我所需要的。我不明白的是 %1 如何仅引用 ([^&]+) ,无论如何它是有效的。谢谢
      • 它引用了第一组括号。如果你有两组括号,你可以使用 %1 和 %2。
      【解决方案3】:

      你会希望你的 RewriteRule 类似于:

      RewriteRule keyword=([0-9a-zA-Z_]+) /catalogsearch/result/?q=%1 [L]
      

      括号内的所有内容都将替换右侧的%1

      【讨论】:

        猜你喜欢
        • 2014-12-03
        • 1970-01-01
        • 1970-01-01
        • 2013-04-05
        • 2017-09-19
        • 2011-02-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多