Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com 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
    
I don't have access to the httpd.conf on the client site. It is a shared hosting setup. –  Charleston Software Associates Jul 9 '11 at 18:39
    
@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

1 Answer 1

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

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

 
discard

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.