Ketan Patel

CakePHP: URL Rewriting





What is Mod_Rewrite?

Simply put, mod_rewrite is an Apache module that let’s you rewrite urls based on rules you define.


Other common uses of mod_rewrite: 
All traffic from multiple domain names can be directed to one domain.
Traffic from specific websites can be blocked.
You can block spammy searchbots and offline browsers from spidering your site and eating your bandwidth.
Mask file extensions can be done.
Image hotlinking (means ,other web pages that links to images that resides on your server) can be Prevented. 

By using the overwrite rule and modifying the htacess file of the root folder CakePHP URLs can be made SEO friendly as shown below.

This is an example of URL Rewrites for the site that has n level menus, 


<IfModule mod_rewrite.c>

   RewriteEngine on

   RewriteRule ^([^/]+html)$ app/webroot/controller_name/function_name/$1 [L]
   RewriteRule ^[^/]+/([^/]+html)$ app/webroot/controller_name/function_name/$1 [L]
   RewriteRule ^[^/]+/[^/]+/([^/]+html)$ app/webroot/controller_name/function_name/$1 [L]
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

Note : In order to run URL Rewrite sucessfully, we need to enable the mod_rewrite.so in the httd.conf of Apache Server.

LoadModule rewrite_module modules/mod_rewrite.so

ketan patel

About Ketan Patel -

I have developed a wide range of websites using CorePHP, Opencart, CakePHP and CodeIgniter including sites for startup companies and small businesses. Apart from my blogging life, I like to read Novels, Listening music and Net surfing.

Subscribe to this Blog via Email :