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'm having problems understanding how RewriteCond directive works. So far, it's pretty clear that it compares to strings to apply a RewriteRule. I have this file:

<IfModule rewrite_module>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app_dev.php

This works for me but I don't know why it works. So far in the RewriteCond directive I understand:

if the value of REQUEST_FILENAME is NOT a file in the hard drive then allow the rule

This doesn't have sense becouse app_dev.php after substituting is a file in the hard drive. Anyways, could someone enlighten me with this issue? I am having a very harsh time figuring out how this works.

share|improve this question

If you requested /styles/main.css then that would be a file on the filesystem and the RewriteRule would not be processed. Apache would simply serve the file.

If you request /foo/bar and that does not exist, the RewriteRule is processed and the request becomes /app_dev.php however, PHP still has access to the original URL, before the rewrite took place. By using that, a framework such as symfony can serve different pages for different URLs, even though the PHP file is the same one every time.

share|improve this answer
ok I see, then what is the value of %{REQUEST_FILENAME}? If I requested the resource /foo/bar the value of %{REQUEST_FILENAME} will be /foo/bar? I don't understand what value is stored in %{REQUEST_FILENAME} – Paulo Bu Jun 1 '12 at 23:58

It's the actual file path on your actual filesystem.

So if your DocumentRoot is /var/www and you request /styles/main.css, then when used in directory/htaccess context %{REQUEST_FILENAME} is /var/www/styles/main.css - and the !-f is checking if that file exists and letting the RewriteRule occur if it does not exist.

Note that it behaves differently and won't have the document root included if it's not used in a directory/htaccess context, as explained in the documentation:


The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI.

share|improve this answer

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.