301 Redirect non WWW. URLs to WWW. Urls

How to instructions using a 301 redirect and mod_rewrite


Simply enter the following code into your .htaccess file (changing the addresses in the examples below as needed) and save the changes. If there are several individual pages which have moved, just repeat as neccesary. Not sure how to access or change or .htaccess file. Read our .htaccess guide here.

 

301 Redirect non WWW. to WWW. Method 1:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]


301 Redirect non WWW. to WWW. Method 2:

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


301 Redirect from WWW. to non WWW domain address

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

Benefits of redirecting traffic to a consistent WWW. or non WWW. domain address

  • Keep your sitewide address consistent, without losing any traffic. Whether your website visitors use the www. or non www. version of your domains web address, the above methods ensure that all visitors will all come through to the same domain address format.
  • It's SEO best practice to prevent canonicalisation problems. It's possible for the www. and the non www. versions of your domain to point to two completely different websites (although this is very rare) using simple DNS zone records. For this reason, there can sometimes be issues with duplicate content if you don't have a proper 301 redirect in place, which can confuse search engines and hurt your rankings. A proper 301 redirect will also ensure that all incoming links to your site are attributed to the one version of your site, rather than half going to the www. version and the other half going to the non www. version.