How to delete .php extension from URL?
How can I delete the .php extension from URL with mod_rewrite?
For example:
test.com/index.php ->开发者_运维知识库; test.com/index/
test.com/contact.php -> test.com/contact/
RewriteEngine on
RewriteRule ^([^/]+)/?$ $1.php
If the user typed in http://example.com/index/
they would get the actual page of http://example.com/index.php
Basically this rule says "match everything from the base url up to a slash, or the end if no slash, but not including the slash. Then give the user that matched part with .php appended to the end."
This is only going to work for the first level of directory; ie. this will not match on example.com/index/some/other/stuff
- no redirect there.
If you really want to redirect requests to /index.php
to /index/
, try this rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ (/[^?\ ]+)\.php[?\ ]
RewriteRule .+\.php$ %1/ [L,R=301]
And for the other direction:
RewriteRule (.+)/$ $1.php [L]
You can also use both rules at the same time to get this behavior:
- requests of
/index.php
are getting redirected externally to/index/
- requests of
/index/
are getting rewritten internally to/index.php
精彩评论