CakePHP mod_rewrite optimization

CakePHP is great, but the default installation has a performance issue that is very easy to resolve, and should be fixed in any production installation of a CakePHP application. The solution is very simple.

In the default install, there is a file in the webroot called .htaccess. It contains mod_rewrite rules, and you have to set you vhost up to allow overrides. This introduces a significant overhead to each request, as apache has to search for applicable htaccess files and merge their settings into the core apache config every request. On a production application this can become significant, especially if you get a significant amount of traffic.

To solve it, simply move the rules from the htaccess file into a Directory entry in the vhost for the site, and set AllowOverride to none. Here’s an example:

<Directory "/path/to/app/webroot">
        AllowOverride none
        Order allow,deny
        Allow from all
 
        # CakePHP Rules
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</Directory>
  1. thats all good when you have access to that, but on shared hosting whats the chance?

    • If you’re in shared hosting, they may still be able to add that block to your vhost entry for you. If not, they’re probably not suited to high load websites. For low – moderate traffic, it’s probably not going to make an appreciable difference, although I would still recommend you do it if possible. It’s only critical for high volume sites.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">