How to Redirect WWW domain to Non-WWW using .htaccess? Set Canonical Page for Better SEO!

Updated on September 1, 2017

I guess, few might have this question already in their mind – why should I redirect www domain to non-www domain or vice versa? The reason is simple – to avoid confusions, at least to non-humans such as search engine bots. Does that mean, a domain www.example.com is different from example.com? Of course, Yes! They are different domains even though they display same contents. In other words, both www domain and non-www domain is going to display the same content, thus resulting in Content Duplication – the serious crime, according to Search Engines.

To make it scary, the issue does not stop there – do you redirect your domain’s IP address to the domain? For example, when http://ip_address_of_domain is accessed, it should redirect to http://domainname.com. It’s called as IP Canonicalization. If it does not, you should check this article to implement IP Canonicalization.

Learn About Canonical Page from Matt Cutts:

So redirecting www.domain.com to domain.com (or) domain.com to www.domain.com is must, but it’s up to you to decide whether you want to redirect all of your www domains to non-www domains or vice versa. Anything is fine, but you should redirect. So that decision is important before you implement the below steps.

The 301 redirection can be done using .htaccess (if you are using Apache HTTP server). To do that, make sure you have mod_rewrite installed and enabled.

SEO optimization

How to redirect www domain to non-www? (Force non-www)

Copy and paste the below lines in .htaccess file:

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

Note: You can either add the above lines in httpd.conf (to do that, you need an administrator access) or in .htaccess file in domain’s root directory. Also remember to change example.com with your own domain name.

(or)

How to redirect non-www domain to www domain? (Force www)

Copy and paste the following lines in .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [nocase]
RewriteRule ^(.*) http://www.example.com/$1 [last,redirect=301]

Note: Remember to change example.com with your own domain name.

Should I do this for every sub domain? Yes, the same configuration has to be done for every subdomain of your website.

Redirection didn’t work via .htaccess?

Check if the web server has disabled .htaccess override.

$ vi httpd.conf

Lookout for ‘ AllowOverride None‘ inside the Document root Directory and change it to ‘AllowOverride All‘ to enable .htaccess overrides.

<Directory "/var/www/html">
 AllowOverride All
</Directory>

That’s it.

Was this article helpful?

Related Articles

Leave a Comment