htaccess issue with directory when removing extension

折月煮酒 提交于 2019-12-24 00:45:53

问题


I use htaccess to remove .php extension from URL. The problem comes up when there's a file and a directory with the same name. By removing .php extension the server thinks I'm trying to access a directory a denies the access.

EXAMPLE www.europe.com/italy.php turns into www.europe.com/italy but as I have a folder named italy, the servers denies the access saying "Forbidden, You don't have permission to access /italy/ on this server"

Here's my 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]

UPDATE: Final code working like a charm!

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] 

来源:https://stackoverflow.com/questions/14908303/htaccess-issue-with-directory-when-removing-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!