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.

The traditional mod_rewrite for routing is as follows:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

However I would like to add one extra condition which is that if a file exists (the -f flag) but that file has a .php extension the rewrite still goes ahead. There are a few ways I've tried to do this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} (.php)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

Using an OR as well as a regular expression to find the .php at the end of the string. This doesn't work (i.e. it loads page.php instead of index.php)

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} .php$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /gadabouting.com/index.php [L]

A different form of the regex causes an Internal Server Error, but with no useful debug information (my current favourite thing to hate about software, poor quality error messages).

Examples:

domain.com/ -> domain.com/index.php
domain.com/string/ -> domain.com/index.php
domain.com/script.js -> domain.com/script.js
domain.com/string/string2 -> domain.com/index.php
domain.com/folder/file.php -> domain.com/folder/file.php
domain.com/file.php -> domain.com/index.php

I.e. for any file/path that does not exist OR any file in the root which contains .php the rewrite rule will be followed

Can anyone point out a rule which will correctly find a .php file and thus rewrite if it finds it in the FILENAME?

Edit: I just found a working solution which satisfies every example except #5. It rewrites any PHP file to index.php even if the file is in a sub directory. Attempts to resolve this have so far been unsuccessful, as the rewrite log doesn't show how it evaluates the RewriteCond directives.

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} .php$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ /gadabouting.com/index.php [L]
share|improve this question

migrated from stackoverflow.com Jan 8 '12 at 11:20

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

    
Could you please add two or three examples of what the user will type in the browser, and how it will be internally rewritten by the server? –  Olivier Pons Jan 7 '12 at 20:48
    
Done, good point. –  M1ke Jan 7 '12 at 20:59
    
The working solution was found by turning on rewrite logging in the httpd.conf file and finding that the rule was redirecting index.php to itself in an infinite loop. However the subdirectory condition still isn't satisfied. –  M1ke Jan 7 '12 at 21:02
    
You should put that as an answer then accept it so the question shows up as answered. –  Jon Lin Jan 8 '12 at 1:50

1 Answer 1

Assuming I'm understanding you, you only wish to rewrite .php files that are in the document root, something like this should do the job...

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} ^[^/]+\.php$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ /index.php [L]
share|improve this answer

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.