简体   繁体   中英

How to use mod_rewrite to rewrite dynamic url for search?

I have a webshop where there is a search field. When I search I am forwarded to a link looking like this:

webshopdomain.com/index.php?route=product/search&keyword= my search phrase

(well, it's more like this: webshopdomain.com/index.php?route=product/search&keyword=my%20search%20phrase )

What do I need to put in htaccess to make this url : webshopdomain.com/search/my%20search%20phrase show the content of the above url?

I have been playing around with this tool ( http://www.webconfs.com/url-rewriting-tool.php ) , but haven't understood how to just grab the search phrase and put that into a static url.

I have also been googling, but in lack of the correct wording, I haven't found the answer.

Thanks :-)

EDIT:

Can I use this? (Just thought out, gonna test...)

Options +FollowSymLinks
RewriteEngine on
RewriteRule search/(.*) index.php?route=product/search&keyword=$1
RewriteRule search/(.*)/ index.php?route=product/search&keyword=$1

To avoid recursion, you need to look at the request line :

RewriteCond %{THE_REQUEST} ^GET\ /index\.php\?route=product/search&keyword=([^&\ ]*)&*([^\ ]+)?
RewriteRule ^index\.php$ /search/%1?%2 [L,R=301]

It was quite simple, but had to rearrange the htaccess file itself.

RewriteRule ^search/(.*)$ index.php\\?route=product/search&keyword=$1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM