Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 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, and realfile.php is a real file, the surfer gets served realfile.php?foo=bar. But if someone goes to 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? 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
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


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.