Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]

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?

share|improve this question

migrated from Jul 9 '11 at 13:33

This question came from our site for professional and enthusiast programmers.

1) How much control do you have over Apache? Can you edit config files, e.g. httpd.conf or httpd-vhost.conf? – LazyOne Jul 8 '11 at 17:58
When you say " Simply changing the extension to .phpt and requesting the same file (with "t" added to the end) brings up the file content." I just want to clarify - what happens when you don't change the extension? Does it say page not found? Does it display the wrong content? – Amy Anuszewski Jul 8 '11 at 18:48
When the extension is not changed (it remains .php) the system hands off to the index.php file for processing, which is incorrect according to the rules in .htacces since blah.php does exist. – Charleston Software Associates Jul 9 '11 at 18:38
@Cyber 1) Is there a chance that you have another .htaccess in a subfolder .. and your requested file is in the same subfolder (or deeper)? 2) Try adding these lines (in case if you do not have them): Options +FollowSymLinks -MultiViews – LazyOne Jul 9 '11 at 21:15
Didn't think about that, multiviews could be an issue. I'll have to check it out next time I'm on the server. Thanks! – Charleston Software Associates Jul 12 '11 at 3:22

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.


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

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

share|improve this answer
The problem is that the file we are asking for IS A FILE it is NOT index.php yet we are still being handed off to index.php regardless. SOMETHING in the Apache config is forcing *php files to ignore the .htaccess rules. Any clues as to what that may be? – Charleston Software Associates Jul 9 '11 at 18:43
Is mod_rewrite more predictable with Cond + Rule? I believe that is an opinion not a fact. It may be less prone to human error but technically it is no more robust. Secondly the original RewriteRule of ^index\.php$ - [L] is FASTER. It is doing a data stack (memory) regex comparison, not querying the file system for data and thus exits the mod_rewrite recursion much faster that way. Removing it in this case provides a less robust solution. – Charleston Software Associates Jul 9 '11 at 18:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.