Populus
Populus

Reputation: 7680

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:

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?

Upvotes: 6

Views: 3785

Answers (2)

Jon Lin
Jon Lin

Reputation: 143906

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).

Upvotes: 6

anubhava
anubhava

Reputation: 785276

Try this rule in your root .htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1/index.php !-f
RewriteRule ^(.*?)/?$ redirect.php [L]

Upvotes: 5

Related Questions