By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Redirecting www to non and non to www

This Tutorial shows how to redirect www to non-www, and non-www to www, using htaccess.

Edited: 2015-12-04 12:30

You can redirect the non-www version to the www version of your site, as well as www to non-www.

Why to Redirect

Both the URLs http://brugbart.com/ and http://www.brugbart.com/ are two unique URLs for the same content, having both of them working could risk counting as duplicate content in search engines.

Examles

Below are two examples, which one to use is up to you. There is no obvious choice, but the www version tend to be used most the time.

Redirecting WWW to non-WWW

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^brugbart\.com
RewriteRule (.*) http://brugbart.com/$1 [R=301,L]

Redirecting non WWW to WWW

RewriteCond %{HTTP_HOST} . 
RewriteCond %{HTTP_HOST} !^www\.brugbart\.com 
RewriteRule (.*) http://www.brugbart.com/$1 [R=301,L]

Note. If you haven't done so already, be sure to include RewriteEngine on, that would become something like:

RewriteEngine on
RewriteCond %{HTTP_HOST} . 
RewriteCond %{HTTP_HOST} !^www\.brugbart\.com 
RewriteRule (.*) http://www.brugbart.com/$1 [R=301,L]

You would only need to enable the Rewrite Engine once.