301 Redirect With No WWW Using Htaccess
The first thing you will need is access to your webroot .htaccess file (located where your index page is). Some of you may not have .htaccess files or are running a single website off your server so you can do this in httpd.conf.
Redirecting With No www
The sample code below will use Apache's mod_rewrite to redirect (R=301) your domain to the same domain without the 'www.' subdomain.
<IfModule mod_rewrite.c>
RewriteEngine on
# 301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
</IfModule>Redirecting To www
The sample code below will use Apache's mod_rewrite to redirect (R=301) your domain to the same domain with with the 'www.' sub domain.
<IfModule mod_rewrite.c>
RewriteEngine on
# 301 redirect to domain to 'www.'
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
</IfModule>301 Redirect With Drupal
For those of you with Drupal simply enter the lines below above the second 'chunk' of directives which wil be identical to your Drupal default .htaccess.
# 301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# Rewrite current-style URLs of the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]More Drupal SEO
To learn more about Drupal SEO techniques follow our Drupal SEO Video Tutorial which will walk you through 5 of the most popular Drupal SEO modules.
Delicious
Digg
StumbleUpon
Reddit
Facebook
Comments
This works great , we were getting search results that led to dead links before this.
Thanks for sharing this informative stuff with us.
this one I always cant understand, thanks for explanations!
Very helpful, thank you
I am thankful for such a great post with many new things to learn. Thanks, you cleared up some things for me.Just what I was googling for! Thanks for sharing!
Now thats how you do it! I have been wondering about this for a while now.
Personally when starting a new site or if you site has little traffic it is just a matter of preference however I stick with no-www.
Thanks for sharing this informative stuff with us
thanks for sharing the useful tips
For an existing site I would suggest looking at your traffic stats and determine which is the most popular via backlinks, and go with that one, otherwise the seo is distributed between both which hurts the site. Personally when starting a new site or if you site has little traffic it is just a matter of preference however I stick with no-www
Thanks for the clear instructions. This is very helpful for a newbie like me.
Is there a clear preference why we should redirect to www or no-www?
Thanks for the clear info, have any tips about how to do so with a multisite setup (only 1 htaccess)
thanks!