How To Redirect From Non WWW To WWW in CakePHP

Hi Friends, Some days ago I was working on one cakePHP project in which I got one problem that I have to redirect domain name from non www to www , means If any one type domain name then it should automatically redirect to It is very easy to do it in any other custom coded website and we can do it by simple htacces rules given below but it is little bit different in CakePHP.

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} ^ [NC]

RewriteRule ^(.*)$$1 [L,R=301]

Now if you want to do this same thing in cakephp then you code will be like this in htaccess exists within app folder.

RewriteEngine on

RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.

RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteRule ^$ app/webroot/ [L]

RewriteRule (.*) app/webroot/$1 [L]

So This is rules for htaccess by which you can transfer non www url to www url in cakePHP. If you have any questions then you can post questions in comment , I will get back to you asap.

Also I am WordPress Developer, Freelance PHP Developer having more than 3 years of experience, So if you want to do any projects then you can contact me.

Hostgator Hosting in 1 Cent Coupon Code – “1CENTHECOUPON” (Without Quotes).

  • ajit siwach

    I use this code but i con’t see result

  • Thanks for sharing this great post Ankur .Waiting for listenning more from you .
    Thanks again

  • Surinder

    This webpage has a redirect loop


    I am getting this issue. could you please help me?