自动URL重写不起作用

时间:2014-09-03 05:06:07

标签: php .htaccess url-rewriting

我想隐藏.php扩展程序,所以我在.htaccess文件中编写了以下代码,我在此链接中找到了How to remove file extension from website address?

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

当我键入localhost / testsite / index时它正常显示localhost / testsite / index.php,并在地址栏中显示localhost / testsite / index,我想要, 但是当我强行键入localhost / testsite / index.php时,它不会转换为localhost / testsite / index。我想删除扩展名,即使用户在页面名称后键入.php。

1 个答案:

答案 0 :(得分:0)

让你的.htaccess像这样:

DirectoryIndex index.php
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,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]