Understanding RewriteCond in .htacces files

.htaccessapache-2.2

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
</IfModule>

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.

Best Answer

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.

Related Topic