Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RewriteCond check if directory exists with an index file

I am trying to do a rewrite for all requests EXCEPT if the URL points to:

  • An existing file
  • A directory with an index file

So I have the following that should have dealt with it:

RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ redirect.php [QSA]

But when I navigate to a folder that contains an index.php (and yes I have checked that DirectoryIndex is set to index.php), it still performs the rewrite.

The funny thing I also found is that if I had a .htaccess in that folder with RewriteEngine on (just that), then the rewrite rule above works...

Any pointers?

like image 776
Populus Avatar asked Oct 29 '13 16:10

Populus


1 Answers

You need to check for the directory part:

RewriteCond %{REQUEST_FILENAME}/index\.php !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ redirect.php [QSA,L]

Also:

The funny thing I also found is that if I had a .htaccess in that folder with RewriteEngine on (just that), then the rewrite rule above works...

This is because rewrite rules in subdirectories have precedence over any rules in parent directories. So if you have a subdirectory with RewriteEngine On, with no rules, then that means that subdirectory has precedence over rewrite rules in the parent directory and there are no rules. (a bit confusing).

like image 152
Jon Lin Avatar answered Oct 03 '22 22:10

Jon Lin