Change URL to .html [closed]

2019-09-21 02:26发布

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.

3条回答
forever°为你锁心
2楼-- · 2019-09-21 02:32

You can try this code :

RewriteEngine On
RewriteRule ^search/([^\.]+).html$ /index.php?searchterm=$1
查看更多
劫难
3楼-- · 2019-09-21 02:35

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>
查看更多
走好不送
4楼-- · 2019-09-21 02:50

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.

查看更多
登录 后发表回答