0

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?

1 Answer 1

1

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.

1
  • 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, 2011 at 19:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .