问题
Here is my current .htaccess code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
it is works only in removing .php extension
from
http://localhost/mysite/news.php?category=cat1&id=1
to
http://localhost/mysite/news/cat1/1/
and from
http://localhost/mysite/news.php?category=cat1&year=2011&month=10&day=25&id=1
to
http://localhost/mysite/news/2011/10/25/1
How to write complete .htaccess for the clean url above?
回答1:
Try This
RewriteRule ^(.*)\/$ $1.php [NC]
for example
http://www.exapmle.com/contact-us/
回答2:
Add an optional / to your pattern:
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]
回答3:
You could just try a simple RewriteRule
:
RewriteEngine on
RewriteRule ^/?(\w+)/?$ $1.php
This tacks on .php
to any file-looking url: example.com/somethin
becomes example.com/somethin.php
, example.com/something/else/
becomes example.com/somethin/else.php
, etc.
The only problem with this is if you try to access an actual folder, like example.com/images
or something.
来源:https://stackoverflow.com/questions/4858253/how-to-remove-php-extension-and-add-slash-on-the-url