Change URL to .html [closed]

烂漫一生 提交于 2019-12-25 18:31:47

问题


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.


回答1:


You can try this code :

RewriteEngine On
RewriteRule ^search/([^\.]+).html$ /index.php?searchterm=$1



回答2:


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>



回答3:


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.



来源:https://stackoverflow.com/questions/17785162/change-url-to-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!