Your site can be accessed via either of the following URLs:
- http://yoursite.com
- http://www.yoursite.com
PROBLEM:
From a search engine's perspective, they are two different sites with exactly the same content. Search engines — Google in particular — will penalize you for this.
SOLUTION:
The solution is to redirect visiting traffic to a single URL using Apache's mod_rewrite. It's your choice as to which version to redirect to as the permanent URL. If you have an ecommerce site and your SSL Certificate is registered to the "http://www.yoursite.com" version, then it would make sense to redirect traffic to the version containing the "www".
Different server configurations require different methods. This article pertains to using an htaccess file and mod_rewrite directives on a Linux server running Apache.
1. Make sure your hosting company has enabled the mod_rewrite Apache module.
2. Create a text file named htaccess.txt and type in (or copy and paste) the following lines of code, depending upon which domain type you wish to redirect from and to:
Example 1 – Redirect domain.com to www.domain.com
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.yoursite.com$ [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]
Example 2 – Redirect www.domain.com to domain.com
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^yoursite.com$ [NC]
RewriteRule ^(.*)$ http://yoursite.com/$1 [L,R=301]
3. Replace the "yoursite.com" in the above code with your actual domain name.
4. Upload the file to the public_html directory of your Linux server.
5. Rename the file from "htaccess.txt" to ".htaccess"
6. Done!
Now when search engine spiders visit your site to update their indexes, they will first crawl the .htaccess page and encounter the 301 redirect instruction sending them to the unified URL.
You can try testing the setup by entering one of the old URLs and seeing if you get redirected.