通过htaccess在url中隐藏文件扩展名

我想隐藏我的文件扩展名在浏览器中显示,像http://www.example.com/dir/abc.php应该只显示为http://www.example.com/dir/abc

我写了下面的rewriteCond并重写了.htaccess规则

 Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+)$ /$1.php [L,QSA] 

但是这个代码似乎不帮助我。

编辑:这是我的htaccess文件的完整代码

 Options -Indexes ErrorDocument 403 /errors/403.php ErrorDocument 404 /errors/404.php RewriteEngine on RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1 [NC] RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1.*$ [NC] RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC] RewriteRule \.(js|css|jpg|png)$ - [F] RewriteEngine on RewriteRule ^(.*)\.[\d]{10}\.(css|js|png|jpg)$ $1.$2 [L] RewriteEngine On # To externally redirect /dir/file.php to /dir/file RewriteCond %{THE_REQUEST} ^[AZ]{3,}\s/+(.+?)\.php[\s?] [NC] RewriteRule ^ /%1 [R=301,L,NE] 

完成.htaccess:

 Options +FollowSymLinks -MultiViews ErrorDocument 403 /errors/403.php ErrorDocument 404 /errors/404.php RewriteEngine on # To externally redirect /dir/file.php to /dir/file RewriteCond %{THE_REQUEST} ^[AZ]{3,}\s/+(.+?)\.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 RewriteRule ^(.+?)/?$ /$1.php [L] # block direct hot linking RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1 [NC] RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC] RewriteRule \.(js|css|jpg|png)$ - [F] RewriteRule ^(.*)\.[\d]{10}\.(css|js|png|jpg)$ $1.$2 [L] 

最简单和最简单的方法:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php [L] 
  RewriteEngine On RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule ^(.*)$ $1\.php 

我不是任何一个Apache专家,但我很好奇为什么没有人推荐谈判模块(mod_negotiation)。 是否有避免使用的最佳实践理由

我通过http://www.webhostingtalk.com/showthread.php?t=317269听说过。 在我的全局配置中启用mod_negotiation之后:

 Loadmodulee negotiation_module /usr/lib/apache2/modules/mod_negotiation.so 

我只需要在我的虚拟主机的选项中启用它。 这是整个配置块:

 DocumentRoot /var/www/site_directory/public_html serverName your.domain.here.com <Directory /var/www/site_directory/public_html> allow from all Options +Indexes Multiviews </Directory> 

简单的方法是:

 you create your link like 

'<a href="home">Home</a>'而不是'<a href="home.php">Home</a>'
那么你的.htaccess应该是这样的:

 RewriteCond %(REQUEST_FILENAME) !-d RewriteCond %(REQUEST_FILENAME) !-f RewriteCond %(REQUEST_FILENAME) !-l RewriteCond ^home home.php [L]