Understand and modify Zend_Framework .htaccess rule

Default Zend_Framework .htaccess rule is pretty hard to understand.
It’s pretty consize and makes intensive use of apache flags.

As time of speaking, current version is:

# public/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

Being this obscure piece of code, all it does is simply redirect to “index.php” all non-existing files.

If all first three RewriteCond statements are true, then first RewriteRule apply, otherwise second rewrite rule apply.

First RewriteRule is simply to do nothing, rewriting url with its own value.
Second RewriteRule simply redirect all uris to index.php (ie. if first rewrite rule did not execute first).

Now let’s see the conditions written here. They all test the same variable (REQUEST_FILENAME) and differ only by the flag they use:

  • -s” return true if REQUEST_FILENAME points to an existing file with a size > 0
  • -d” return true if REQUEST_FILENAME points to an existing directory
  • -l” return true if REQUEST_FILENAME points to an existing link/shortcut

Since they are all linked using [OR] statement, if any of these three tests is true, first RewriteRule (the one that does nothing) gets executed, otherwise second RewriteRule (the one that redirect to index.php) gets executed.

That’s it! Now we have a better understanding of this.

I found out that the browser was automatically querying for “favicon.ico” file. Since for now I did not want to put any favicon, no such file existed and therefore was redirected to index.php, generating a 404 error within my logs (note: this error is not visible on the site since the browser ask for an image and therefore does not display any returned error).

Problem is that it polluted my logs a lot (on each page request!). As a quick fix, I simply modified .htaccess file, asking for not redirecting to index.php if requested file is favicon.ico.
Below is my .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} favicon.ico$ [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

sources

Leave a Reply