htaccess重写规则并删除php文件扩展名

时间:2014-07-29 03:15:11

标签: regex apache .htaccess mod-rewrite

我在htaccess文件中有以下代码来删除PHP扩展:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

但是,现在我在/directory/index.php中添加了一个文件,我只能使用/ directory / index访问它,而不是只使用/ directory /(现在抛出404未找到)。

1 个答案:

答案 0 :(得分:1)

在你的根目录中保留这样的规则.htaccess:

DirectoryIndex index.php

RewriteEngine On
RewriteBase /

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]