我使用 htaccess 从 URL 中删除 .php 扩展名。当存在同名的文件和目录时,就会出现问题。通过删除 .php 扩展名,服务器认为我正在尝试访问一个拒绝访问的目录。
示例 www.europe.com/italy.php变成www.europe.com/italy但由于我有一个名为 italy 的文件夹,服务器拒绝访问说“禁止访问,您无权访问 /italy/服务器”
这是我的 HTACCESS
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
更新:最终代码就像一个魅力!
DirectorySlash Off
IndexOptions FancyIndexing
Options All
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^.]+\.php
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.yoursite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(([^/]+/)*[^/.]+)$ /$1.php [L]