Php – mod_rewrite !-f and !-d ignored for .php extensions

.htaccessapache-2.2mod-rewritePHP

I have an .htaccess file on an Apache 2 server:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

The rules work as expected with any request for a non-existent file or directory being handed off to the index.php program for processing.

The rule also bypasses the redirect if a file being requested exists, such as any of the .txt or .js files on the server.

However any file ending with .php does NOT process properly. The files exist but the !-f and !-d rules are ignored. Simply changing the extension to .phpt and requesting the same file (with "t" added to the end) brings up the file content.

Any clues why only .php files would skip the !-f and !-d rules?

Best Answer

The mod_rewrite work more predictable with a pair: Cond + Rule Your line:

RewriteRule ^index\.php$ - [L]

Already match with following line because index.php is a file.

Try:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
</IfModule>

I guess that . will match with only one character and I changed . to .*

Related Topic