Connaxis Creative Outsourcing - Stay Competitive - Outsource Web Design to Argentina

Home » Blog
Blog
lang_btn lang_btn

CakePHP: Using mod_rewrite without .htaccess overhead

Added by:19 July 2009 16:59

A quick solution for all of you who want to disable .htaccess files on their server but still use mod_rewrite with CakePHP:

<Directory /var/www/mysite>
AllowOverride None
Options FollowSymLinks
RewriteEngine on
RewriteRule  ^$ /app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/app/webroot.*
RewriteRule ^(.*)$ /app/webroot/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^app/webroot(.*)$ /app/webroot/index.php?url=$1 [QSA,L]
</Directory> 

If .htaccess files are enabled (e.g. AllowOverride All) apache needs to check the directory path for ".htaccess" files on EVERY request. This means a significant overhead for high-traffic sites as traversing the directory path and opening the ".htaccess" files are not for free. The above code can easily be put in a apache virtual host configuration to prevent the .htaccess overhead and still have nice SEO-friendly Cake URLs.

 
mai
Posts: 4
Comment
Re:Please help me. It dosn't work.
Reply #4 on : Thu August 06, 2009, 15:58:04
Hello,again.
When I wrote the code like below, it worked.
If you know something from this situation, please let me know.
Thank you!

<Directory /var/www/mysite>
AllowOverride None
Options FollowSymLinks
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</Directory>
<Directory /var/www/mysite/app>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</Directory>
<Directory /var/www/mysite/app/webroot>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</Directory>
mai
Posts: 4
Comment
Please help me. It dosn't work.
Reply #3 on : Thu August 06, 2009, 08:19:22
This is the very tips that I've wanted to know.
I copied the above code and paste to my httpd.conf but it didn't work -- 404 Not Found error appeared. I confirmed that my application work when I use .htaccess file like cakePHP Manual.
Please let me know if there are another points for using the above tips?
Abba Bryant
Posts: 4
Comment
Don't forget
Reply #2 on : Fri July 31, 2009, 20:38:00
to add the Directory directives for the webroot/css etc folders also.
jamesF
Posts: 4
Comment
will try this tip
Reply #1 on : Thu July 30, 2009, 05:37:45
seems like it could save a second or two