December 16, 2010

What my htaccess looks like in Zend Framework

While coding under Zend Framework there's a standard for what htaccess files should look like. Using mod_rewrite every request is checked against the filesystem for existance, and then served. If it does not exists, the control passes to index.php who starts the Zend Application.

Since, even nowadays, every disk access is paid in valuable time, I made my own htaccess, optimized for disk avoidance whenever possible. After all, it's only a convention to follow, isn't it?

This is the common htaccess as seen in a vast majority of Zend Framework applications (also seen here):

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

mod_rewrite is not rocket surgery, but it's as close as one could get. This rules are checked for every request (^.*$)

My daily htaccess is as follows:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule \.\w{2,4}$ - [NC,L]
RewriteRule  . index.php [NC,L]

In fact it's pretty simple: Only requests ending in dot and 2 to 4 letters should be checked against the filesystem. Serve the file if they match and pass to index.php in any other case.