Redirect WWW to non-WWW using Apache (htaccess)

The easiest way to redirect all your traffic from a WWW address (like www.domain.com) to a non-WWW (like domain.com) is using the following code:

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

With this code, you don’t need to hard-code the name of the domain into the .htaccess file.

Apart from the fact that www is deprecated (more here), the main benefit of doing this (or redirect from non-WWW to WWW) is that Google and other search engines won’t penalize our site because of duplicated content on both versions of our domain (WWW and non-WWW).

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? *