htaccess重写删除子目录上的php扩展

时间:2015-04-26 04:58:56

标签: apache .htaccess mod-rewrite

我的htaccess文件位于此文件夹下:

http://localhost/project/folder/
|
| ---改变这样:
|
| _ http://localhost/project/folder/about.php
| ____ http://localhost/project/folder/about
| ____ http://localhost/project/folder/about/
|
| _ http://localhost/project/folder/anything.php
| ____ http://localhost/project/folder/anything
| ____ http://localhost/project/folder/anything/
|
| _ http://localhost/project/folder/file.php?id=8888
| ____ http://localhost/project/folder/file/id/8888
| ____ http://localhost/project/folder/file/id/8888/
|
| _ http://localhost/project/folder/file.php?edit=8888
| ____ http://localhost/project/folder/file/edit/8888
| ____ http://localhost/project/folder/file/edit/8888/

到目前为止我找到了什么代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteRule file/edit/(.*)/ file.php?edit=$1
RewriteRule file/edit/(.*) file.php?edit=$1 

RewriteRule file/id/(.*)/ file.php?id=$1
RewriteRule file/id/(.*) file.php?id=$1


# Remove file extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

我面临的问题:如果我尝试,http://localhost/project/folder/about正在工作,而不是http://localhost/project/folder/about/

1 个答案:

答案 0 :(得分:0)

您可以使用此.htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule ^ /%1 [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^file/edit/([^/]+)/?$ file.php?edit=$1  [L,QSA,NC]

RewriteRule ^file/id/([^/]+)/?$ file.php?id=$1 [L,QSA,NC]

# Remove file extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]