Forum Moderators: mack
In Google Webmaster Tools, this is what it tells me for ""In external links to your site"
1. http www example com
2. http example com
3. example com
Should I add to my .htaccess file this?
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
This is my current file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$ [OR]
RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$ [OR]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$ [OR]
RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$ [OR]
RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$
RewriteRule ^/?(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301,L]
[edited by: eelixduppy at 5:00 am (utc) on Jan. 7, 2009]
[edit reason] exemplified [/edit]
First, you should remove the "$" from the end of all hostname patterns, in case someone requests the FQDN by appending a period, or in case they append a port number. This is a perfectly valid hostname: www.example.com.:80
Second, you can cut the number of lines in half by replacing the pairs of www- and non-www lines with one line making the "www." optional:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [OR]
Third, if you have no domains pointed to this server that *should not* be redirected to the canonical domain, in other words, if all non-canonical domains pointed to your server should be redirected, then you can replace that whole pile of RewriteConds with one line:
# Redirect to the canonical domain if request is not for *exactly* the canonical hostname
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Jim