Redirect incomming links with .htaccess

How do I redirect all links for www.domain.com to domain.com ?

Description of the problem

Your website can be accessed with www.domain.com and domain.com. Since Google penalizes this due to duplicated content reasons, you have to stick your domain to either www.domain.com or domain.com.
But – since some links are outside of your website scope and the search engines already have indexed your website under both addresses, you can’t change that easily.

Solution

Do a 301 redirect for all http requests that are going to the wrong url.

Example 1 – Redirect domain.com to www.domain.com

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

Example 2 – Redirect www.domain.com to domain.com

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

Explanation of the .htaccess 301 redirect

What does this code above do?

Let’s have a look at the example 1 – Redirect domain.com to www.domain.com. The first two lines just say apache to handle the current directory and start the rewrite module.

The next line RewriteCond %{HTTP_HOST} !^www.domain.com$ specifies that the next rule only fires when the http host (that means the website of the queried url) is not (- specified with the “!”) www.domain.com. The ^ means that the host starts with www.domain.com, the $ means that the host ends with www.domain.com – and the result is that only the host www.domain.com will trigger the following rewrite rule. Combined with the inversive “!” is the result every host that is not www.domain.com will be redirected to this domain. The [NC] specifies that the http host is case insensitive.
The escapes the “.” – becaues this is a special character (normally, the dot (.) means that one character is unspecified).

The next – and final – line describes the action that should be executed: RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]. The ^(.*)$ is a little magic trick. Can you remember the meaning of the dot? If not – this can be any character(but only one). So .* means that you can have a lot of characters, not only one. This is what we need – because this ^(.*)$ contains the requested url, without the domain. The next part http://www.domain.com/$1 describes the target of the rewrite rule – this is our “final”, used domain name, where $1 contains the content of the (.*). The next part is also important, since it does the 301 redirect for us automatically: [L,R=301]. L means in this is the last rule in this run – so after this rewrite the webserver will return a result. The R=301 means that the webserver returns a 301 moved permanently to the requesting browser or search engine.