www. vs. no-www. – Canonical URL Fixes

March 27, 2009

EDIT: This is BY far my most popular post.  If you find this useful, please feel free to link to it or my home page.  Thank you!

So you’ve got your domain – http://www.site.com – and it works just fine and displays your information with either the www. prefix or without it (http://site.com vs http://www.site.com).  The kicker is you’re actually serving 2 duplicate versions of your website – and that’s bad, mmmkay?  You’re having canonicalization issues and you need to fix them.

4 characters, the “www.” prefix literally serves a new URL string and likewise a new website to a search engine bot, so having 2 versions of the same site on the web is not recommended.  Every search engine loathes duplicate content with the heat of a thousand supernovas so in order to serve only one version of your website, you need to make a few updates.  Or at least get your hosting provider to make them, if possible.

You need to 301 Redirect (permanently move) all requests to 1 URL version.

If you’re running a windows server, you’ll need to make an IIS update.

If you’re on a Linux machine, you’ll need to make an update to your .htaccess file.

For the sake my own sanity, I’m going to utilize redirecting all http://site.com traffic to http://www.site.com.

For Windows Users:

Follow every step HERE, it works perfectly. (check below for the next little bit of advice when you’re done though, so come back!)

I’d post my own steps and info, but that link has it down perfect.  Basically, you need to create an IIS reference for http://www.site.com and one for http://site.com.  Then, you set the reference for http://www.site.com to reference the files on your server.  Next, set the non-www. reference to 301 permenantly redirect to http://www.site.com as described in the seablick link – remember to add “$S$Q” to your domain.  You may need to update the Host Header to include http://www.site.com as well in the non-www. reference , which can be found under Properties -> HTTP Headers Tab -> Advanced button – > enter http://www.site.com in the appropriate field.

For Linux Users:

Your task is MUCH easier.  You simply need to create a file named – just like this – .htaccess – and upload to your home/index server space.  It must be in the exact directory location as your index page and it must be named “.htaccess” or it will not work at all.  You can create one named “htacces.txt” in Notepad, upload it, and rename accordingly.  You might need to check with your hosting provider to see if you’re allowed to make such updates as well, some people have super secure hosting providers.  You may also need check and make sure that Apache’s mod_rewrite.c turned ON.  I promise, this is still WAY easier than Windows’ version.

In your .htaccess file you simply need to put these few lines of code in, save it, and you’re done:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]

That’s it, all the code needed, copy, paste, save, done.

Like I said, the Linux lovers have it easy.  That’s all the code needed to change any and every request for http://site.com to http://www.site.com AND it will capture any and all page references as well.

Static Pages: http://site.com/contact.html will redirect just fine and dandy to http://www.site.com/contact.html

Dynamic Pages: http://site.com/index.php?method=569877 will redirect to http://www.site.com/index.php?method=569877 as well.

There, now you’ve got 1 version of your URL on the web.  The search engines will surely thank you for it and your users will too.

I promise, this by far the most popular error people have with their websites.  Canonical domains rank far better and have less errors than non.  It’s the most common suggestion on any SEO forum when someone is screaming to find out why their site has suddenly dropped in the rankings.  The answer is simple: you’re confusing the search engine as to which version of your site is ideal.

WAIT!  WAIT!  WAIT!  BUT WHAT ABOUT LINKS!!!

Word on the street is 301 redirects pass link juice/love, but I doubt they pass 100% the same way a non-redirected link does, but that’s a discussion for another post – mental note.  If you can, you should get in touch with sites that are linking to you and politely ask them to make a little update for you, particularly any big important really SEO helpful links.  If you can log in to a directory where you’re a member, take a minute and make the update.  And going forward, try to get links using one URL version.

Another EDIT: Chances are the below individual was having issues because of the $1 added to the end of several lines of code specified above.  You might try taking the 1 off, or the $ as well if you’re having issues.

Advertisements

14 Responses to “www. vs. no-www. – Canonical URL Fixes”

  1. Paris Vega Says:

    What a gold mine of information! Gee, thanks, Kyle!

  2. kjernigan Says:

    You’re welcome. I just got tired of ALWAYS having to reference 2 different resources for these types of fixes. So I just decided to make my own.


  3. […] internally, or externally, to anything other than “http://www.sitename.com” – if your canonical domain is http://www.sitename.com.  If you’ve got links on your site to .com/index.html – and that […]

  4. Dan Says:

    Thanks for the info it really helped

  5. Paritosh Says:

    Hi,
    The most easy explanation i have got on the internet while searching for the same !
    Thanks for such a nice post.
    Since, i am not a tech savvy person, i have a query, the codes you have mentioned above need to placed in the existing .htaccess file or do i have to delete the previous one and uploaded new one containing these lines ? And if these codes have to placed in the existing file then where should they be placed ?
    Any help would be much appreciated.
    Thank you.

    • kjernigan Says:

      You can post the code in the same .htaccess file you’ve created, just below any code that’s currently in there.

  6. Paritosh Says:

    I did but i need to redirect www to no www and i guess the code give above does the opposite.
    Can you provide a way to redirect www to no www.

    Thanks

    • kjernigan Says:

      Sure, just swap the non-www. URL with the www. URL – here ya go:

      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^www.site.com$ [NC]
      RewriteRule ^(.*)$ http://site.com/$1 [L,R=301]

      • Paritosh Says:

        Nah still cant redirect it to non www url.

        Here is my .htaccess file, take a look at it maybe you can help

        # BEGIN WordPress

        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]

        # END WordPress
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^www.techvorm.com$ [NC]
        RewriteRule ^(.*)$ http://techvorm.com/$1 [L,R=301]

      • kjernigan Says:

        Try this instead:

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

        Sometimes you have to keep playing with your update to get it just right. Hope this helps!

  7. Paritosh Says:

    I am totally new to this and have no idea about it !

    Anyways thanks for your time, will get back to you after testing this.
    Thanks again !

  8. Paritosh Says:

    Again the same problem, no redirection to techvorm.com

  9. Lucy Says:

    Great post. Thanks for the simple explanation and for excluding the complicated technical jargon.

    I will now try and implement the redirect.

    Thanks again.


  10. […] simple.  There’s a ton of information all over the web on how to do it.  I cover how to 301 redirect users to a canonical domain here, and 301 redirecting dynamic pages to a new, more static type URL string isn’t far […]


Comments are closed.

%d bloggers like this: