3

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?

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. – Lance Cleveland Jul 9 '11 at 18:38
  • 2
    @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
    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! – Lance Cleveland Jul 12 '11 at 3:22
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 .*

  • 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? – Lance Cleveland 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. – Lance Cleveland Jul 9 '11 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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