RewriteCond to skip actual directories seems to be ignored
-
30-04-2021 - |
Question
My problem is that The RewriteRule
is still matching when I visit a physical directory, e.g. http://a-domain.com/foo/
where foo
is a normal directory in the web root.
the .htaccess
file has:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .? index.php [L]
It works fine on my dev server but not on a live WHM/CPanel server. I'm a bit lost.
Solution
I've tested this on my cpanel website and you're right and I found out where's the problem. when you try to access a folder in your website that doesn't have default file (index) , it tries to access the file that is responsible for 403 HTTP code, and because that doesn't exist, it rewrites URL to index.php
.
All you have to do is add this to above of your .htaccess
file:
ErrorDocument 403 /index.php?type=err&code=403
OTHER TIPS
I tried this on a VM which mirrors my own shared service and it works fine for me. Have you got another .htaccess
in the foo
directory? If so, then the rewrite engine will ignore your one in DOCROOT and use this instead?
The /foo/
dir has basic authentication so it was failing when %{REQUEST_FILENAME}
contained a non-existent file /home/the_user/public_html/401.shtml
. I didn't think this was an issue because the basic authentication worked, and when I cancelled the auth prompt I was being served a standard 401 file. Usually when those ErrorDocument files are missing, Apache says Additionally, an error of type 404 was encountered while trying to use an ErrorDocument to handle the request.