这是我当前的.htaccess代码
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
它仅适用于删除.php扩展名
这
http://localhost/mysite/news.php?category=cat1&id=1
至
http://localhost/mysite/news/cat1/1/
和
http://localhost/mysite/news.php?category=cat1&year=2011&month=10&day=25&id=1
至
http://localhost/mysite/news/2011/10/25/1
如何为上面的干净网址写完整的.htaccess?
答案 0 :(得分:3)
答案 1 :(得分:2)
为您的模式添加一个/:
RewriteEngine On
# rewrite news articles and pass news id as a GET parameter to news.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/mysite/news/(\d+)/?$ /mysite/news.php?newsid=$1 [NC,L,QSA]
# rewrite all other page requests to .php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/mysite/(.*)/?$ /mysite/$1.php [NC,L,QSA]
答案 2 :(得分:1)
您可以尝试一个简单的RewriteRule
:
RewriteEngine on
RewriteRule ^/?(\w+)/?$ $1.php
对于任何看起来像文件的网址.php
,example.com/somethin
会变为example.com/somethin.php
,example.com/something/else/
变为example.com/somethin/else.php
等等。
唯一的问题是,如果您尝试访问实际文件夹,例如example.com/images
或其他内容。