Zend Framework, Rewrite Rules, Apache

Most of the Zend Framework documentation I’ve read recommends a rewrite rule that looks something like this:

RewriteEngine on
RewriteRule !\.(swf|js|ico|gif|jpg|png|css)$ index.php

Why not use a more elegant rule that looks like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php

This rule basically says: If it’s not an existing file, and it’s not an existing directory – pass the request through index.php.

This allows you far more flexibility when you want to host non-framework files, or a whole slew of other media types that are not listed in your apache configuration.

You can leave a response, or trackback from your own site.

5 Responses to “Zend Framework, Rewrite Rules, Apache”

  1. arthurmaciel says:

    For sure this an ellegant solution, but using the first one you have specific control of what can be accessed. Any try to access files or directories forbidden should be redirected to the index.php and the framework should display an 404 error.

  2. I suppose that is a matter of preference. I don’t keep anything ‘forbidden’ inside of my document_root for my applications.

    As a rule, for myself, if it’s inside document_root – it’s supposed to be accessible.

  3. Pokero says:

    Thanks Darryl, I was getting a bit tired of editing htaccess every time I wanted a non-framewrk file published!

  4. [...] the following, as suggested here. This has the major advantage that you can now add non-framework files easily to your public [...]

  5. [...] An elegant approach suggested by Darryl Clark involved writing the rule to redirect if the path is not an existing file or directory then route it to index.php.  This approach is particularly helpful in my case because I wanted to have my Zend_AMF gateway available in a file named service.php. I’ll have to road test it and update this with my results. [...]

Leave a Reply

Subscribe to RSS Feed Follow me on Twitter!