CakePHP: URL Rewriting

, , Leave a comment

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]

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

LoadModule rewrite_module modules/