htacess redirect non-www to www or other way around

A simple htaccess code to redirect your website from www to non-www or vice versa. It's important concept in SEO because most of the search engine will treat www and non-www as separate website. As a result, you want all the traffics focus on a single domain name, thus, this htaccess code is a good snippet to know.

Apache

from non-www to www

RewriteEngine on
RewriteCond % ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

Apache

from www to non-www

RewriteEngine on
RewriteCond % ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301

Share the love

Leave a Comment

Please keep in mind that comments are moderated and rel="nofollow" is in use. You can use [code][/code] if you want to write codes. Don't spam us :) Thanks!

Advertisement