Skip to main content

.htaccess Mod-Rewrite for WWW Domain Name Prefix

Posted in
Doing a 301 redirect for domain names with regards to www prefix. If you have a domain name, i.e., you may want to use only to display your site instead of, or vice versa. To do so, it is very simple, as long as your server supports mod_rewrite and allows users to configure the .htaccess file to do a redirection. (read more...)

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 to use the www-prefix, Slashdot chooses to use instead of whereas Digg allows the use of either or Therefore, what is the underlying reason for doing so, or not doing.

Generally, webmasters tend to improve site ranking. A domain name, ie., which can be accessible via and is interpreted as two different sites by the search engine. For example, search for Digg in Google, and you will find that and 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 to, insert the following code into your .htaccess file.

# mod_rewrite in use
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^ [NC]
RewriteRule ^(.*)$$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 (.*)$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

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.

Welcome travolsta

Hi travolsta, welcome to the site. Hope you enjoy your stay here with us.

See you around.

very useful article.

Hello, I found here through search engine, I find some very useful articles here.
Good joh.

Ted, thank you

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

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

Can you show me the file that you've written in .htaccess?


Glad to hear that you've managed to solve the issue :) Good luck to your site.

mode rewrite insertion

hi i'm new in programming

could it be possible to insert word in rewrite rule



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]



just the code i needed and you were number 1 on google for:

htaccess redirect www

cheers dude

Great redirect info

You make it simple to do this stuff. I had to do a redirect not only from to 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.