Most sites can be reached by two different URLs. For example http://b2evolution.net/about/features.html and http://www.b2evolution.net/about/features.html .

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.

Explanations=

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 somedomain.com to www.somedomain.com


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 “http://domain.com” to “http://www.domain.com” you need this code:

rewritecond %{http_host} ^domain.com [nc] rewriterule ^(.*)$ http://www.domain.com/$1

2008-07-26 19-47

Comment from: Kassra

Where does this code go? I have a godaddy.com 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 godaddy.com 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 “http://www.domain.com” to “http://domain.com”?

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

Hi,

Could you help me?

I want to redirect www.example.test.com to http://example.test.com.

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.

Excellent!

2012-02-19 00-30

Comment from: isimsar.com

I tried it but it doesn’t change the address to www.domainname.com

2012-09-03 14-56