Redirecting all subdomains to the main domain

Tagged lighttpd Tagged apache

Using the canonical hostname for websites isn’t an obvious problem. After all, who really cares if your site is accessible at www.domain.com and domain.com? Search engines, for example, could penalize you for having the same content on both domains. I think that today they are smart enough to discover this specific case though.

Another problem can happen when you use cookies. If not done correctly, your user’s cookie won’t be available to the website when they were stored under www.domain.com and he uses domain.com the next time he visits. See the cookie specs for more details.

A quick research led me to a solution for apache that I didn’t like too much. It involves using mod_rewrite. mod_rewrite is a great tool, but there’s a better solution for redirecting to your main domain from subdomains or second level domains. Simply use a catchall virtual host in your apache host configuration file(s) to do the redirects. This will solve all the SEO, caching and cookie issues, and it will save some CPU cycles compared to the mod_rewrite or higher level solutions.

# NameVirtualHost *:80
 
<VirtualHost *:80>
    ServerName www.domain.com
    # This is your main domain
</VirtualHost>
 
<VirtualHost *:80>
    ServerName domain.com
    ServerAlias *.domain.com
    # This is to make sure that foo.domain.com gets redirected too
    # If you want to use more virtual hosts on subdomains,
    # just define them earlier
    Redirect / http://www.domain.com/
</VirtualHost>

Please refer to the mod alias docs to decide which Redirect you need, this example uses a 302.

For lighttpd you can use this:

$HTTP["host"] =~ "domain\.com" {
    $HTTP["host"] != "www.domain.com" {
        url.redirect = (
            "^(.*)$" => "http://www.domain.com$1",
        )
    }
}

By the way, if you’re an IIS user, you might want to read this.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Related posts:

  1. Mailman, HTTP POST and 301 redirects
  2. Using any Debian box as a wireless access point
  3. Git clone, ssh: Could not resolve hostname

8 Comments

  • Posted by Peter on 2. April 2010 at 21:19.

    maybe i am wrong, but as far as i know you have to write HTTP["host"] !~ “www\.domain\.com” in your lighty example?

  • Posted by trecords on 25. April 2010 at 08:38.

    Hi,

    Is it possible to redirect subdomains of all parked domains to its own main domain?
    Ex i have 1000 parked domains and want to redirect traffic to their subdomain to certain domain. I don`t want to make rule for each domain, is it possible to make it generally for cpanel?

    Thanks.

    • Posted by Nicolas on 25. April 2010 at 11:03.

      That sounds easy enough to do, but I’m not sure about doing it with cpanel as I’ve never used it. It kind of depends where the DNS points to as well, do you host the domains on your own server, use an external domain parking service, etc. I’m also not sure what precisely you want to do, but http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectmatch looks like a good place to start.

  • Posted by Phil on 22. June 2010 at 16:08.

    Note to self: “>” is actually “&gt;” in html. Lighttpd does not understand that in its configuration. lol.

    • Posted by nicolas on 22. June 2010 at 18:51.

      Yeah. I blame the broken wp import/export filters. Fixed :-)

Leave a Reply

Your email is never shared. Required fields are marked *

*
*