How To Redirect From Non WWW To WWW in CakePHP

Posted by Ankur Gandhi in CakePHP



CakePHP_img

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 http://example.com then it should automatically redirect to http://www.example.com. 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} ^yourwebsite.com [NC]

RewriteRule ^(.*)$ http://www.yourwebsite.com/$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).

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

  • ajit siwach

    I use this code but i con’t see result

  • http://oopeducation.com/category/php/ PHP TUTORIALS

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