Thursday, January 21, 2010

cakephp mod_rewrite optimizations

A few people on the wordpress forums were discussing ways to improve the performance by editing the .htaccess file. Since WP and cakephp along with other PHP frameworks use a similar method of improving URLs, here's my attempt at improving cakephp's ./webroot/.htaccess

# optimizations
RewriteEngine On

# images in these dirs tells mod_rewrite to stop processing
# '-' means to pass request unchanged. ie serve it immediately
RewriteRule ^(img|css|js)/(.*)\.(gif|jpe?g|png|ico)$ - [L,NC]
# these other files are served as is anywhere in ./webroot
RewriteRule \.(swf|css|js)$ - [L,NC]
# If File is a file or is a DIR then skip (1 rule) and serve the file
# (vs before where it was !-f AND !-d Not a file AND not a dir, two checks vs less than two if the first one matches)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=1]
# else rewrite the request
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

No comments: