First of all, before jumping onto the wagon and following what others do, you have got to ask yourself why do you want to do it? There are several reasons why people do that, and why people don't. For example, Microsoft redirects microsoft.com to use the www-prefix, Slashdot chooses to use slashdot.org instead of www.slashdot.org whereas Digg allows the use of either digg.com or www.digg.com. Therefore, what is the underlying reason for doing so, or not doing.
Generally, webmasters tend to improve site ranking. A domain name, ie. domain.com, which can be accessible via www.domain.com and domain.com is interpreted as two different sites by the search engine. For example, search for Digg in Google, and you will find that digg.com and www.digg.com exist. Although this improve visibility in search engine, it also has its drawback. Having two sites mean, the ranking of that domain is divided (unequally; dependent on inbound and outbound links) into two. It is fine if the site is highly ranked, and users have no problem discovering the site. However, for sites which do not have very high ranking, it is perhaps more ideal to confine to using one particular domain name for accessing the content.
While a lot of webmasters are highly interested in improving sites' Search Engine Optimization (SEO), Page Rank (PR) and Search Engine Result Page (SERP) for business, others are just happy to create sites for personal use. If you belong to the latter, then it is perhaps not so important to worry about www-prefix on your domain names, unless you feel that it looks nicer with (or without) the www-prefix; otherwise, this article might just be useful for you.
To perform a redirect from domain.com to www.domain.com, insert the following code into your .htaccess file.
# mod_rewrite in use
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
To perform a redirect from www site to non-www site, use the following code in .htaccess file.
# mod_rewrite in use
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain.com/$1 [R=301, L]
The .htaccess file is simply a text file with the filename .htaccess, and it should be stored at the root directory of your site.
Note, while using .htaccess file, it is important to use it carefully. Wrongly inserted command may render your site not workable.




Hi every one
Submitted by travolsta (not verified) on Thu, 2007-02-15 23:39.
- reply
hey just dropping a line to say waht's up. new girl on site and in america...was out of the country for a few years.
hi!
Welcome travolsta
Submitted by keith on Tue, 2007-02-20 16:38.
- reply
Hi travolsta, welcome to the site. Hope you enjoy your stay here with us.
See you around.
very useful article.
Submitted by Ted (not verified) on Thu, 2007-05-24 14:55.
- reply
Hello, I found here through search engine, I find some very useful articles here.
Good joh.
Ted
Ted, thank you
Submitted by keith on Thu, 2007-05-24 15:19.
- reply
Hi Ted, thank you for the nice compliment. I find that your social networking site is pretty good too. Keep up the good work!
i give up! htaccess issues
Submitted by be awesome instead (not verified) on Tue, 2008-02-05 03:54.
- reply
So I have tried what you suggested and can't seem to get it to work. I haven't changed my htaccesss file from the original that came with Wordpress so I'm not trying anything special.
at first I forgot the [R=301,L] line so my site stayed up but the non www was not redirected to www.
Once I put that line in my site vanishes... fun eh?
Any ideas what I am doing wrong? I suppose my other option is doing a 301 redirect.
Thanks for the advice.
Show the File
Submitted by keith on Wed, 2008-02-06 04:26.
- reply
Can you show me the file that you've written in .htaccess?
written .htaccess
Submitted by be awesome instead (not verified) on Sat, 2008-02-09 17:59.
- reply
keith, thanks for the offer of help. (by the way, expect a linkback soon) I went ahead and upgraded to the latest version of Wordpress that handles this for me but for kicks I'll show you what I was doing so you can have a laugh. After looking at a friends .htaccess file I figured out that I was being well....dumb. Either way, here it is and thanks for the help:
# mod_rewrite in use
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^beawesomeinstead.com [NC]
RewriteRule ^(.*)$ http://www.beawesomeinstead.com/$1 [R=301,L]
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Cheers
Submitted by keith on Sun, 2008-02-10 13:15.
- reply
Glad to hear that you've managed to solve the issue :) Good luck to your site.
PHP Alternitive
Submitted by NeoZiggy (not verified) on Sat, 2008-02-23 17:26.
- reply
Of course this code should be the first thing in your index page (ex: index.php). I am assuming you use "?page=" to call pages, if not change the word "page" to what ever you use where ever you see "$_GET['page'];" below. I whipped this togeather really fast and thought maybe other people could use it.
$host = $_SERVER['HTTP_HOST'];
if ($host == "www.DOMAIN-NAME.com") {
if (isset ($_GET['page'])) {
$page = $_GET['page'];
header("Location: http://DOMAIN-NAME.com/?nz=$page");
} else {
header("Location: http://DOMAIN-NAME.com/");
}
}
mode rewrite insertion
Submitted by Guest (not verified) on Tue, 2008-04-01 04:58.
- reply
hi i'm new in programming
could it be possible to insert word in rewrite rule
Ex www.xxx.com/hot-in-time.html
to
www.xxx.com/cheap-hot-in-time.html
i want to insert cheap for prefix could it be possible
the rule in .htaccess is
RewriteRule ^item/(.*)/(.*)/$ item-page.php?item-id=$1&keyword=$2 [L,NC]
thanks
thanks
Submitted by tristan blackburn (not verified) on Mon, 2008-11-10 16:22.
- reply
just the code i needed and you were number 1 on google for:
htaccess redirect www
cheers dude
Great redirect info
Submitted by Dana Labrie (not verified) on Wed, 2009-12-30 20:59.
- reply
You make it simple to do this stuff. I had to do a redirect not only from domain.com to www.domain.com but also redirects from HTML to SHTML. Kudos on your easy to understand info. It took me 4 hours and lots of hard to understand info to finally be able to fix this issue. As a web developer specializing in SEO, I am also going to impliment for all my customers since I didn't realize that is one of 100 SEO issues that most sites face.
Thank you for making a very
Submitted by WinniG (not verified) on Wed, 2010-01-20 17:19.
- reply
Thank you for making a very informative articles. I can surely put this into good use since lately, I have been experiencing difficulties with my domain. Hope this will help.
Post new comment