2014-09-03 135 views
1

我想隐藏PHP扩展,所以我写了下面的.htaccess文件的代码,我发现这个链接How to remove file extension from website address?自动URL重写不工作

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

这项工作很好,当我键入localhost /测试网站/索引呢正确显示localhost/testsite/index.php并在地址栏中显示localhost/testsite/index, ,但是当我强制键入localhost/testsite/index.php时,它不会转换为localhost/testsite/index。即使用户在页面名称后键入.php,我也想删除扩展名。

+0

恐怕你不能。我拥有超过一般的htaccess文件的经验,它的工作方式与您的写作方式相同。你仍然可以访问完整的路径,而不是重写,如果你手动编写它,我没有看到任何错误! :) – Cowwando 2014-09-03 05:09:16

+0

这是一个重复http://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess – jbrahy 2014-09-03 05:11:53

+0

在例如stackoverflow站点如果我写 stackoverflow.com/questions/25636602/automatic-url -rewriting-未working.php?noredirect = 1个#comment40055522_25636602 它将显示 stackoverflow.com/questions/25636602/automatic-url-rewriting-not-working?noredirect=1#comment40055522_25636602 和将除去PHP扩展。 – 2014-09-03 05:18:44

回答

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]