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 want my RewriteRule to not take effect if there is an actual file matching the surfer's request within the file system. So if someone goes to mysite.com/realfile.php?foo=bar, and realfile.php is a real file, the surfer gets served realfile.php?foo=bar. But if someone goes to mysite.com/nonexistentfile.php and nonexistentfile.php is not a real file, then the RewriteRule should interpret and pass the request to process.php

I have the following in my .htaccess:

 RewriteEngine on
 RewriteRule (.*)\.php$ process.php?id=$1 [QSA]

This works IF there is no real file in the file system matching the surfer's request. It does not work if the surfer enters realfile.php?foo=bar...in that case they hit the RewriteRule. It does work if they just do realfile.php, but I want query strings still get passed to the real file if it exists. I thought the local file system always took precedence over RewriteRule?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I might suggest something such as the following (similar to what WordPress uses):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

The -f flag checks for an existing file, the -d flag checks for a directory - so it will apply the rewrite if the request is not for a file or directory.

share|improve this answer
    
RewriteCond %{REQUEST_FILENAME} !-f is what did it, thanks! For some reason I thought it just defaulted to always serving the local file if it exists- guess not! –  Phil Jul 11 '11 at 19:18

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.