| « The importance of URIs | Sitemaps priority is confusing » |
How to redirect www.domain.com to domain.com
03/21/07
How to redirect www.domain.com to domain.com
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 wether 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.
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 (.+) .
13 comments
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
"RewriteCond %{HTTP_HOST} !^$"
because the second RewriteCond line that checks for the domain name already checks for an empty HTTP_HOST variable.
What are your thoughts on wildcard redirect for directories?
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!
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.
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.
Excellent!
