Set domain to www or non-www

Sometime its seen that the domain can be opened with or without www, like for example, if your website is example.com then it could be opened in www.example.com as well as example.com right? But some scripts like WordPress do redirect to www automatically when you set the correct path in Site Address.

Well, for users having this issue, what if you only want www.example.com and want to redirect example.com to www.example.com? So with a little tweaking of your current “.htaccess” file in your webserver with the below code

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)://www.example.com/ [L,R=301]

Please make sure you edit example.com with your domain name!

Ok, So what if you want the other way round? ie., www.example.com to example.com? Then use this code below!

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)://example.com/ [L,R=301]

Well this would do the job!




September 3, 2012
/
Previous Post Next Post

Tharun recommends you to read these fantastic articles