Redirect Visitors From Non-WWW To WWW Version Of Your Website With .htaccess And mod_rewrite

Apache’s mod_rewrite module is a very powerful tool. And there’s some neat tricks you can do with it. This trick involves forcing visitors and search engine robots to either the WWW version of your website, www.yourdomain.com, or the non-WWW version of your website, yourdomain.com.

This is probably not something you want to change up after your site has been up for a while, but something you should think about in the development stages of your website. Changing this after you’ve already been indexed can have some unwanted efffects, like loss of PageRank or a complete removal and then reindex of your website by the search engine bots.

To accomplish this neat trick, we’re going to edit—or create—an .htaccess file and add just three lines of code to it. For those of you that don’t know, an .htaccess file is a simple ASCII file that you can create in something like Notepad. The actual name—or rather, the extension—of the file is .htaccess. It’s not file.htaccess, or index.htaccess, or anything else, it’s simply named .htaccess.

Ok, so here’s the code you need, and I’ll start with sending visitors to the WWW version of your website:

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

Pretty simple. And here is how you would send visitors to the non-WWW version of your website. I use this method on divspace, and if you were to try and type the URL to this site with a WWW in front, you would notice that it would automatically redirect you to the non-WWW version. Anyways, here is the code:

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

Once you have decided on which version of your site you want your visitors to go to, save the .htaccess file and upload it via FTP in ASCII mode to your website. For most users out there, you’ll be uploading this either to your public_html or www directory. It does not matter which one you choose, as they are both the same thing.

Also, if you’re going to do this, and you have an XML version of your sitemap that you manage with Google’s webmaster tools, then make sure you specify to Google what your preferred domain is. You can find this option by clicking on the site you want to manage, and then on the sidebar on the left, there is a link for “Preferred domain.” Simply click on this and let Google know if you want to have your site indexed and listed with (or without) the WWW.

Related Articles

18 Responses to “Redirect Visitors From Non-WWW To WWW Version Of Your Website With .htaccess And mod_rewrite”

  1. Tim Linden Says:

    I’m one of those people that prefer the www, so I made a plugin for wordpress. It’s easier for me that way, I just upload it with all my other plugins every time I install wordpress and don’t have to think about editing it ;-)

    (It uses PHP to redirect though, not htaccess, but still does the same effect)

  2. Kyle Says:

    It’s funny you mention that plugin, because after I wrote this I found out there was a plugin too. My only problem though was the fact that it was in PHP, and from what I hear, it’s much easier and faster to let your .htaccess file handle it.

    I personally like the www in front of my domains, but with divspace, I didn’t want it for some reason. But yeah, all my other domains use it, they feel naked without them :)

    Another reason why I chose to talk about this in the .htaccess file is because I use it a lot. For example, sometimes I need to use PHP4 in one directory, and PHP5 in another. And I have a lot of custom mod_rewrite rules that I use too.

    Anyways, I’ll be expanding more on the power of the .htaccess file down the road, I haven’t had any time to write anything because I’m working on the redesign of this website.

  3. Adan Says:

    Is there any special techniques that can be used to optimize a wordpress blog on my server for SEO. One issue I see is no way to change the title tags on each page, where it seems to take the blog name for the home page.
    I have several hundred 600+ inbound links.
    I have pinged Technorati manually and used pingoat as well as pingomatic every time I add a new blog.
    There is plenty of content, about 30 articles.
    What else can I do? What else should I do to optimize my blog?

  4. Josh Kaufman Says:

    Is there an advantage or disadvantage either way or just a personal preference? I just registered a new domain for a new project I am starting and I am trying to figure out which way to go.

    My first impression is that you want everything redirected to Non-WWW just because many people don’t type if WWW. I know if i wanted to visit divspace i would simple type in ‘divspace.com’

  5. Kyle Says:

    Do you mean is there an advantage over using mod_rewrite to to redirect pages or PHP, or using a WWW and not using one in front of the domain? For the first, the advantage definately goes to mod_rewrite.

    As for WWW versus non-WWW, it totally depends on the crowd that’s coming to your site and your personal preference. Obviously you should force a user one way or the other, because I notice that search engines and even when users bookmark your site in social bookmarking places, they do a combination of: using the WWW, not using the WWW, using a trailing slash after directories, and not using trailing slashes.

    So pick two options and stick with them. Either force them to a WWW version or don’t. Either force a trailing slash or don’t.

    You would think that most people don’t type the WWW, but I would beg to differ. We’re not most people. People like my parents type the WWW, and I actually don’t type the WWW either, but I do the shortcut Ctrl + Enter and that automatically ads a WWW and COM before/after whatever word you type in the address bar.

  6. Pudding Says:

    I have a website, that i paid easyspace to put in a redirect service for my www. domain name (wish i hadn’t now after reading this!) However i was wondering, when you have a redirection like this, how you go about making your favicon show up in the address bar? My long domain, when accessed directly, shows it, but when i use the www. it doesn’t.

  7. Kyle Says:

    It should show up either way, the WWW wouldn’t make a difference. I would have to see what you are doing.

  8. Kevinpan Says:

    It seams good idea.
    I’ll try it on my site.
    thank you writer.

  9. huckle Says:

    Nicely explained. Too often are the mysteries of mod_rewrite overcomplicated by poor authorship. As for non-www or www, you contradict yourself by saying you prefer to use non-www in the article body yet in th title you say “From Non-WWW To WWW Version”. My own opinion is that the uninitiated and less geeky audience use www, but in time people work out they can be lazy.

    I use firefox, so don’t even bother with the tdl if I can get away with it; most often google redirects you straight to the site you want anyway. If there isn’t one that matches you can just click it from the results page. Also helps with spelling mistakes as google is great at suggesting alternatives…

  10. huckle Says:

    Sorry to post twice, I said tdl not tld in my previous post. Apologies.

  11. gakyi Says:

    thank you for this www tip.

  12. George Says:

    Great tip. Apache rewrites are great, but sometimes a royal pain.

  13. Marco Says:

    Great tip. Works perfectly for what I needed. Everyone should think about doing this because it’s better for seo to use one domain or the other. Not both. Otherwise, you can get treated as 2 different sites. Google does a pretty good job of knowing, but the lesser known sites won’t give you as much credit for backlinks if your links are spread across different domains, e.g.: domain.com vs. www.domain.com

  14. Guido Bouman Says:

    This can be made even better. I was developping a CMS that has to run on multipile domains, so to make it work I hade to mak my .htaccess work with every domain.

    For forcing www.:
    [code]RewriteEngine On

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

    And for stripping it:
    [code]RewriteEngine On

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

  15. robin Says:

    Hi, can someone please help me?

    I’ve been trying to redirect beka.co.uk to a www.beka.co.uk for a while now and am having no luck.

    The following is the first three lines or my .htaccess file…

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^beka\.co.uk$ [NC]
    RewriteRule ^(.*)$ http://www.beka.co.uk/$1 [R=301,L]

    The site is running on an apache server, but i don’t have access to http.conf file, is this where my problem lays?

    Any advice would be greatly received as this is starting to bug me

    Cheers

  16. John Says:

    Well written and much appreciated. I’m finally getting around to this on a site that I purchased almost two years ago. You say that “Changing this after you’ve already been indexed can have some unwanted efffects, like loss of PageRank or a complete removal and then reindex of your website by the search engine bots.”Is that still the case, and if so, what do you recommend? This site has been online since 1997! Thanks again. -jp

  17. SEO Basic Guide and Techniques Says:

    That’s a great idea I’ll try it as well

    Thanks for the post

  18. Jesse James Says:

    Thanks for posting this info. I am doing research on mod rewrites and how they effect SEO so all the information that I can get is appreciated.

Leave a Reply