将查询字符串重写为另一个查询字符串

时间:2015-05-23 02:46:33

标签: regex .htaccess url mod-rewrite rewrite

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

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

并将其更改为:

 /catalogsearch/result/?q=47174

我需要在" keyword ="之后取值。在&之后忽略一切签名并在?q =

之后将其提供给第二个网址

这是我到目前为止所提出的:

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

然而,这也打印了网址末尾的关键字=,不打印q =或清除&

之后的所有内容

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

你会想要你的RewriteRule:

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

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

答案 1 :(得分:1)

您可以使用以下内容:

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

其中$1是对捕获的组1的后向引用(此处为关键字的值)

答案 2 :(得分:1)

试试这个:

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