2010-02-06

Redirect www.domain.com to domain.com

In my post "Redirect domain.com to www.domain.com", I described how to redirect requests of domain.com to www.domain.com (note the addition of www.).

It's also possible to do the reverse. In this post I'll describe how to redirect requests from www.domain.com to domain.com (note the removal of www.).

Twitter.com uses this "base domain" technique. Try it... access http://www.twitter.com/c3scripts and you'll see that you're redirected to http://twitter.com/c3scripts

Note: Throughout this post, I mention domain.com (without www.) and www.domain.com (with www.). They are different. Be very clear on that point.

Note: Some web servers are configured to only handle accesses to www.domain.com and will not accept accesses to domain.com. Try accessing your own domain http://domain.com (without www.) and see if your server can handle it or not. If not, ask your hosting company to configure the server to accept domain.com (without www.) accesses, otherwise you cannot use this redirection on your server.

Adding two statements to your root .htaccess file is all you need to do. Add these statements near the top of your .htaccess file before all other Rewrite statements.

#--- remove www domain prefix ---
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*) http://%1/$1 [R=301,L]

So what do these two lines do?

The RewriteCond (a rewrite condition) tests the HTTP_HOST (what domain is being accessed) to see whether it has www. at the start. If that condition is met, then the next statement is evaluated. If that condition is not met, then the RewriteRule is skipped.

The RewriteRule takes whatever path is being accessed and redirects to that path at the base domain. For example, http://www.domain.com/something.html would be redirected to http://domain.com/something.html (without www.). In the RewriteRule, the %1 is substituted with what the RewriteCond matched in its parentheses, and the $1 is substituted with what the RewriteRule matched in its parentheses. So %1 has the base domain, and $1 has the path. So, http://%1/$1 is the URL of the base domain and path.

Note: If you have no root .htaccess or it has no RewriteRule statements, then somewhere above the preceding RewriteCond/RewriteRule statements, you also need these two statements to ensure that the Rewrite engine is enabled:

#--- enable Rewrite engine ---
RewriteEngine on
RewriteBase /

So, if you are using one of our DySE scripts, such as DySE::StubHub, then your .htaccess file might look something like this:

#--- enable Rewrite engine ---
RewriteEngine on
RewriteBase /

#--- remove www domain prefix ---
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*) http://%1/$1 [R=301,L]

#--- DySE::StubHub at root / ---
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.+$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ cgi-bin/dyse/view.pl?merch=stubhub&dir=&path=$1 [L]

The order of these three blocks of statements is important. You want the test of the domain name to be done before all other RewriteCond/RewriteRule statements.