If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Most website URLs have the www-prefix, like www.domain.com. Some opt not to use it, but have their URL as simply domain.com. There are different opinions on which is better, but this post is not about choosing what to use. This post will show you how to make sure both addresses will work if the visitor types it in, and that search engines use the correct one.

It is quite easy to set both domains to work, but after both www.domain.com and domain.com take the visitor to your site, you want the address bar to show your preferred address. This way, if they decide to link to your site, they’ll use the correct URL. If you are using a Wordpress blog, and want to do it easily, see Bryan’s post about it. This post shows how to do it with a htaccess file.

A .htaccess file is a directory level configuration file with Apache web servers. It’s not available on all hosts, because not all use Apache, and some have decided not to use it for performance reasons. You can see if your directories contain a file with the name .htaccess if you don’t know whether your host supports it, or just check their FAQ. If you don’t find the answer, just try it: nothing will happen it it is not supported.

Create a text file with the name .htaccess (just a dot and htaccess, no extension after that) in your top level directory, if one does not exist. Insert the following text, if you want to redirect from the non-www address to the www-one:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Use the second one, if you wish to use the non-www address:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain.com
RewriteRule (.*) http://domain.com/$1 [R=301, L]

Test it out immediately, and if the site won’t display, delete the file, or the changes you made to it. Some hosts don’t allow ModRewrite, and may display an error message.

by Sutocu | September 6, 2007

RSS feed | Trackback URI

Leave a Comment»

Comment by Pat
2007-09-06 16:14:00

I also use .htaccess for redirecting my affiliate links.

 
Comment by Sutocu
2007-09-06 19:35:00

Yes, that’s another good use for it.

 
2007-11-15 14:34:39

[…] be redirected to an address without it (after setting a cookie). Also, you should pick either the WWW version or the non-WWW version of the domain, and redirect the […]

 
2007-11-25 05:25:06

[…] that he can make a technical post more easy to digest, if you don’t believe me check how to Redirect Domain Names With htaccess; also you may like Natural Link Building with Link Baits and my favorite: How to Make Get-Paid-To […]

 
Comment by free ipod classic
2007-12-28 01:47:27

thanks for the post, i’m going to have to do more homework on htaccess

 
Comment by dreamluverz
2008-05-19 11:57:20

hmmm… what could be the prob. I used as stated on the example but didn’t work. :((

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.