.htaccess redirection

egarding the .htaccess example on p23, I find that the the !-f rewrite rule is not enough to avoid unnecessary redirecting from happening; it is very common to have links to non-existing javascript, styles or images, and whenever that happens a new call is made upon index.php due to the redirection.
If you have but mywrongimg.gif does not exist, the request will be redirected to the bootstrapper. This should be avoided as it can cause performance issues with your ZF application.

To solve the issue, I use the following rules for my .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/css
RewriteCond %{REQUEST_URI} !^/img
RewriteCond %{REQUEST_URI} !^/js
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1

That way, if any files or directory exists (!-f & !-d), it will not be redirected to the bootstrapper. Also if a file does not exist within the /css, /img, or /js folder, Apache will return a standard 404 error instead of redirecting the request to the bootstrapper.


The problem I have with
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php
is that whenever you want files which do not have those extensions to be handled directly by Apache, you will have to amend the RewriteRule. Using it in combination with the -d and -f rules will solve that, but then sometimes you will want a .css or .jpg, etc. file to be created by the Zend Framework (i.e. redirected to the bootstrapper).
For example:

where someimage.jpg is created on-the-fly by the ZF… that would not be possible with the aforementioned Rewrite rule.

On the other hand, using a rule such as
RewriteCond %{REQUEST_URI} !^/img
will create “safe heavens” for your images (avoiding unnecessary redirection when referencing non-existant files) and at the same it gives you the flexibility to create .jpg/gif/png files on-the-fly.


I agree.

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

is far too limiting for anything other than the simple case. The “safe havens” idea is clever; I usually just turn off the Rewrite engine in that directory. Your method saves having to have multipel .htaccess files around.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: