Using Mod-rewrite to Redirect Non-www URL to www URL

Did you know the URLs “www.The SEO” and “http://The SEO” are distinct URLs and are treated differently by the search engines?

This has to do with URL canonicalization. What’s URL canonicalization you ask?

According to the head of Google’s Webspam team, Matt Cutts, URL canonicalization is “the process of picking the best url when there are several choices”:

* www.The SEO (same as http://www.The SEO
* The SEO (same as http://The SEO
* www.The SEO
* The SEO

Most people think that those are the same, but on technical term, they are NOT!

*Can you see how the URLs are different, even though they may serve up the same web page?

Luckily, after a major update to “Big Daddy,” Google now is able to detect which one is the best URL for you/your website. However, you do have to help Big Daddy in the process.

How do you help make sure Google pick the URL that you want?

Well, for your internal linking, the URL that you use should be ‘consistent’. For example, if I’m planning to use “http://The SEO” as the consistent URL form for all of my EXTERNAL inbound links, it’s wise to use that URL form in ALL my internal linkings, as well. And, as you can see, I have done just that.

*Note: the reason you don’t want to have half of your links pointing to “” and the other half to “” is because you do NOT want to split the value of those links up. You want to have one ‘consistent’ URL form, so that your website (page rank) will receive the FULL benefits of ALL the links, not just HALF of them.

–But here’s the problem, you only have control over the URL form of those external/internal links if you are the one who’s placing those links (URL form). For many other people who link to your website, you cannot control which URL forms (http://www. or http://) they will link to you.

Thus, in order to receive FULL benefit of consistent URL, you need to set up a 301 permanent redirect from one URL form to another, which is pretty easy–as shown below:
Using Mod Rewrite to Redirect “Non-www” URL (http://) to “www” URL (http://www.)

In order to do this, you must have access to the .htaccess file via FTP or CPanel File Manager. Most Apache web server (Linux hosting) has this .htaccess file. If not, you can create and name the new file .htaccess and place it inside the root directory of your domain (e.g. public_html/.htaccess).

You must edit/add the following mod-rewrite script to your .htaccess file to redirect “http://” to “http://www.”:

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

The above script will redirect: to


Redirecting “www” URL (http://www.) to “Non-www” URL (http://)

This is the simple mod rewrite script I use to redirect “http://www.The SEO” to “http://The SEO“:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [nc]
RewriteRule (.*)$1 [R=301,L]

*Note: you should change “The SEO” to YOUR WEBSITE NAME! Also, if you have sub-domains, the above script will also work. Also, by ‘default’, I think WordPress gives your site URL as “” If you want your site to be in “http://” URL form, you, of course, have to go to “OPTION ->General” and update your site URL to “” instead of “”

Anyway, here is a live demo of those two scripts at work: >>> >>>

By using either one of these mod-rewrite scripts, you can have a consistent URL in your linking campaign. Now, you don’t have to worry what URL form others have used to link to your website. Now “http://” and “http://www.” are ‘consistently’ the same, although I think “http://” is a better URL form for your SEO linking campaign, just because that form is SHORTER than “http://www.” 😛 .

Related posts on 

If you like "Using Mod-rewrite to Redirect Non-www URL to www URL,"
please consider linking to this page:

« Previous
« Why Yahoo New Robots-Nocontent Tag Will Have Little Use | Up Top | WordPress SEO Tutorials »
Recent Comments