Nitro
Nitro

Reputation: 43

Rewriting query string to another query string

I need to take search urls that are passed to my site like this:

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

and change them into this:

 /catalogsearch/result/?q=47174

I need to take the value after "keyword=" ignore everything after the & sign and give it to the second url after the ?q=

This is what I have come up with so far:

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

This however, prints keyword= at the end of the url as well, does not print the q= or clean everything after the &

How can I fix this?

Upvotes: 4

Views: 48

Answers (3)

karthik manchala
karthik manchala

Reputation: 13650

You can use the following:

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

where $1 is the back reference to the captured group 1 (here.. value of keyword)

Upvotes: 1

Amit Verma
Amit Verma

Reputation: 41249

Try this :

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

Upvotes: 1

Martin Konecny
Martin Konecny

Reputation: 59701

You will want your RewriteRule something like:

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

Everything inside the parenthesis will replace the %1 on the right side.

Upvotes: 1

Related Questions