301 Redirects on the Drupal .htaccess file for non-www to www

How to redirect non-www requests to the www.site to reduce risk of SEO penalty

Numerous posts regarding SEO mention that search engines may punish
sites for having 'duplicat content' even if that apperant copy is on
your own site.

For example a site might be available to both https AND http or
http://www AND non-www. Due the magic of the internet this could appear
to be two sites with the exact same content. Engines don't like seeing
content copied for many of the same reasons that we don't like and
often fear copies in the real world. Let's face it duplicates can be
scary and can be the cause of spammers putting up multiple copies of
info just to reap on banner ads.

Well for all of you Drupal fans out there, here is the code I put
into my htaccess file in the root of the drupal install directory.


  •  
      # Modify the RewriteBase if you are using Drupal in a subdirectory and
      # the rewrite rules are not working properly.
      RewriteBase

      #Below in the 301 redirect from non-www to www
      RewriteCond %{HTTP_HOST} ^theworldsgreatestguy.com [NC]
      RewriteRule ^(.*)$ http://www.theworldsgreatestguy.com/$1 [L,R=301]
      RewriteCond %{HTTP_HOST} ^www.worldsgreatestguy.com [NC]
      RewriteRule ^(.*)$ http://www.theworldsgreatestguy.com/$1 [L,R=301]
      RewriteCond %{HTTP_HOST} ^worldsgreatestguy.com [NC]
      RewriteRule ^(.*)$ http://www.theworldsgreatestguy.com/$1 [L,R=301]

  • # Various rewrite rules.

    RewriteEngine on


I have this alter ego site that I have been Dr Jekylling in the
evenings and as you can see I have two URLs for the same site and I
wanted all derivitives of them to point to
http://www.theworldsgreatestguy.com

I know that this may not be brain surgery but I hope it helps a few
people understand how to do it for themselves. So many of the SEO
articles I had read just said this was a good idea without spelling it
out. So here it is. I am sure there is a way to merge some of the code
to make it cleaner but this GTD.