Most sites can be reached by two different URLs. For example and .

This is good for the users who can choose whether or not to type in the “www.” part. However, it is not good for search engines which will tend to see a lot of duplicate contents.

If you are on an Apache webserver, you can use mod_rewrite in order to redirect all traffic that goes to the www.domain to the “non www” domain. (If you want it the other way round, see below)

Try adding this to a file named “.htaccess” at the root of your site (create that file if it doesn’t exist):

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

Note: More advanced users will prefer to add this to the httpd.conf / apache2.conf instead in order to gain a little efficiency and also to not interfere with tests on development/staging servers.


RewriteEngine on activates mod_rewrite.

The first RewriteCond makes sure we have a non(!) empty(^$) hostname to work with.

The second RewriteCond detects that the host name is not(!) the one we want (e-g: it has an extra www. part in it).

The RewriteRule sends out a permanent(301) redirection to the right domain name followed by the currently requested path/page ($1).

Handling multiple domains at once=

Below is more elegant (yet complex to read) solution that can handle multiple domains at once (if you have “ServerAlias"es) and that doesn’t require to type-in your domain (just copy/paste):

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

The trick here is to use %1 which matches the canonical part of the domain name in the second RewriteCond (.+) .

Reverse redirection=

By popular demand, here is how to redirect from to

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

Comments from long ago:

Comment from: Feo

Very nice exapmle! And if I would like to use opposite syntax? To redirect to www-version?

2007-11-22 23-02

Comment from: SimonSimCity

This is great !!! I’ve many problems with the .htaccess-Passwords … If I include a picture with the WWW I had to type in my Passwords again … Thanks for this great fix !!!

@Feo If you want to redict “” to “” you need this code:

rewritecond %{http_host} ^ [nc] rewriterule ^(.*)$$1

2008-07-26 19-47

Comment from: Kassra

Where does this code go? I have a account for my domain and I have an A record set up posting to hosting elsewhere. Is it possible to set this up through my account?

2009-01-29 12-01

Comment from: Narayana

You actually do not need the line “RewriteCond %{HTTP_HOST} !^$” because the second RewriteCond line that checks for the domain name already checks for an empty HTTP_HOST variable.

2009-08-05 08-12

Comment from: traffic to your website

Can’t we just use the cpanel perm 301 redirect to accomplish the same purpose?

What are your thoughts on wildcard redirect for directories?

2009-12-28 06-15

Comment from: traffic to your website

Forgot this one…isn’t it best policy to go “” to “”?

I am wondering why FEO above would want to go in the opposite directions.

Is there are good reason (besides personal preference) to do go in FEO’s direction?

Thanks for your reply!

2009-12-28 06-19

Comment from: Dinesh

Very nice examples, every beginner website owner asking for this code.

2010-07-26 08-07

Comment from: alpine mountaineer maudit review

I am curious to find out what blog platform you have been using? I’m experiencing some small security issues with my latest site and I’d like to find something more risk-free. Do you have any recommendations?

2010-12-31 00-53

Comment from: Rokabear

Nice Examples. Finally got mine working because of them.

2011-05-07 04-27

Comment from: Bali Fashion

I wonder if this works for Wordpress multisite system?

I’m still struggling with Wordpress 3+ multisite and prefer to having www for the multisite’s main domain. Can’t find the solution yet and will appreciate if you’d be able to guide me :-).

Thanks a lot.

2011-08-30 16-29

Comment from: jayendra


Could you help me?

I want to redirect to

I tried the solution you mentioned here but this did not work.

2011-11-14 12-51

Comment from: SEO Expert Company

good stuff

2012-02-05 19-12

Comment from: Maz

Thanks Francois! Used the second option for handling multiple sites and it worked like a charm for me.


2012-02-19 00-30

Comment from:

I tried it but it doesn’t change the address to

2012-09-03 14-56