How can I change this URL
http://mysite.com/index.php?searchterm=my+great+song
to
http://mysite.com/search/my-great-song.html
I am a beginner in mod_rewrite. That is why I can not figure out what to do. I would appreciate your help if you can tell me the .htaccess code that I should write in the .htaccess file to change the url.
You can try this code :
RewriteEngine On
RewriteRule ^search/([^\.]+).html$ /index.php?searchterm=$1
I tried its work
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^index.html$ index.php
RewriteRule ^search/([^\.]+).html$ /index.php?searchterm=$1
Options All -Indexes
<files .htaccess>
order allow,deny
deny from all
</files>
The following htaccess should work for you
RewriteEngine on
RewriteCond %{ENV:stripspaces} 1
RewriteRule ^(.*)%20(.*)$ $1-$2 [N]
RewriteCond %{QUERY_STRING} ^searchterm=(.*)$
RewriteRule ^index\.php$ /search/%1.html? [E=stripspaces:1,E=redirect:1,N]
RewriteCond %{ENV:redirect} 1
RewriteRule ^(.*)$ $1 [R,E=!stripspaces,E=!redirect]
I use environment variables [E=a:b]
(docs) to control when spaces should be stripped and to prevent extensive amounts of redirects. The N
flag makes the rewriteengine start from the top of the .htaccess file again. Therefore I put the stripspaces rule in the top, to minimize the amount of rules that need to be matched. The %1
in the second rules is a backreference to the first capture group in the rewritecondition. The trailing ?
clears the query string. The documentation for all this can be found on httpd.apache.org.