301 Redirect NON WWW to WWW (or vice versa) - Good SEO technique!

NobleCloud

New member
Some of you might wish to discover a really good SEO technique that is so simple to do. And yet helps your page rank on search engines such as google.

By default, you see, your site resides on both http://yourdomain.com and http://www.yourdomain.com

This means that search engines are going to take twice as long to index you as they are indexing content from both the WWW domain and the NON WWW domain. This will affect your page rank and will slow down the length of time it takes for you to get a PR quite considerably.

Also when people link to your page. Some might link to the NON WWW domain and others to the WWW domain. Basically this would cause your page rank to be different for each as some might link more on one than the other. In other words you're creating double the work for yourself and the search engine bots. The quick way to fix this is to permanently redirect one to the other. See below. :)

So, how to fix this? Well, I'm going to show you how you can create a 301 redirect from http://yourdomain.com to http://www.yourdomain.com (NON WWW to WWW). You can do this the other way round, and redirect from the WWW to the NON WWW if you wish.

The technical term for this type of redirect is "Canonical Redirect".

All you need to do is create a .htaccess file and place it in the PUBLIC_HTML folder (make sure you call it "."htaccess, with the period...think of it as a .htaccess extension. Whatever you do, don't upload it as htaccess.txt!). You will also need to make sure that mod_rewrite is enabled on your server (you can ask your hosting provider about this if you're not sure).

Once you've done that simply add the following code to the .htaccess file. This code will tell your site to redirect any visitors who visit your site without inputting the WWW to your WWW domain. Anyway, I don't want to confuse you any more. So here's the code...make sure you change yourdomain.com to your domain. ;)

Code:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^yourdomain.com [nc]
rewriterule ^(.*)$ http://www.yourdomain.com/$1 [r=301,nc]
 
Careful with that code, you can find yourself in a redirect loop.

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

The above is the one that should be used.
 
Surprisingly enough, for me anyway, the code I stated is the only one I've ever got to work WITHOUT getting myself into a redirect loop. I've tried many codes before. The one I use has worked out the best, surprisingly.

So I'm afraid I'll have to disagree with you on that one. Sorry. :(
 
No worries. If it works for you, that's all that matters.

The reason for the extra !^(.*) has to do with subdomains. The last thing you'd want is a subdomain for your same domain being redirected off to some other location. So by entering that portion, that ensures that your subdomains don't get fired off to the main domain. It also saves time on your Server (which means faster website for your visitors) as the system does not need to check to see if "www" is present or not. In your example, even if WWW exists, it will rewrite the URL. That's an extra few milliseconds that could be used to download data to the visitor rather than wasting time looking up to redirect.

Every millisecond counts.

Also, adding the "L" on the last line rather than an "nc" (which has to do with case), the L means it's the last line to process for that Rewrite Condition. It helps the server know that it doesn't need to keep reading the rest of the .htaccess file for this particular redirect.
 
That's useful to know. Thanks. :)
However I'll stick to my current setup for now as it's the only one I've ever got to work. ;)
 
Nice post.
Google count different www or without www both type of websites. With www is a standard form of website. If your server show your domain name without www, you can use 301 Redirect to redirect your website without www to with www domain name.
 
Careful with that code, you can find yourself in a redirect loop.

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

The above is the one that should be used.


where should this code be placed?
 
you don't have to place at the top of .htaccess, just place it anywhere but make sure it's the .htaccess in your website root.
 
Yes.
If you have, for example, Frontpage extensions installed on your domain you could put at the bottom (or at the top) of your .htaccess file in root. ;)

So, it can go anywhere in the .htaccess file providing, as hostloc said, that it's in the root .htaccess (or the default one in your public_HTML folder). ;)
 
So what do you guys prefer, anyway? Redirecting to a WWW adres or a non-WWW adres? So far on my personal sites I've always used the non-www adres since I feel the WWW in a URL has become kind of obsolete. But what is the best route to take and why?
 
Personally, we keep the "www" in the domain name. You'd be amazed at how many NEW people to the web still type the "www" in the domain names. If you setup your redirection correctly and also tell google webmaster tools (and bing tools) the correct format for your domain (with or without the WWW) then you'll have no issues no matter what way you do it. You just need to pick one way, and stick with it.
 
Back
Top