Zakalwe
Zakalwe

Reputation: 1622

mod_rewrite picking up directories that exist

I have the following .htaccess in the root folder of a web application:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?req=%{REQUEST_URI}&%{QUERY_STRING}

This works as I want with one exception: if the directory actually exists, that directory is served rather than the request being passed of to the PHP script.

So if directory /docs/ exists, the index.htm is sent to the browser whereas a request for the directory /doesnotexist/ which doesn't exist will be handed to the PHP script.

Any ideas how I can amend this so that ALL requests are handed off to PHP regardless of whether the location exists or not?

Upvotes: 2

Views: 92

Answers (1)

aefxx
aefxx

Reputation: 25279

Your second set of conditions constrain matching requests to not point to an existing file nor an existing directory. That's why when you browse to /docs/ and the directory exists the rule won't fire.

All you need to do to fix this, is to get rid of the second rewrite condition. This let's the rule beneath it fire on any request that does not point to an existing file. If, indeed, what you want is to redirect every request to the PHP script, then the first rewrite condition needs to be eliminated as well.

RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?req=%{REQUEST_URI}&%{QUERY_STRING}

Note that the # uncomments the line so it won't get picked up by mod_rewrite.

Upvotes: 1

Related Questions