Redirecting all subdomains to the main domain

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.

8 comments

  1. avatar
    wrote this comment on

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

  2. avatar
    wrote this comment on

    In the second line? Your line would work as well, but I don't need a regex to match, so != will do.

  3. avatar
    wrote this comment on

    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.

  4. avatar
    wrote this comment on

    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.

  5. avatar
    wrote this comment on

    ah sorry, you are right! :-) but i meant the dot-masking .. is it not neccessary when not using regex?

  6. avatar
    wrote this comment on

    no :-)

  7. avatar
    wrote this comment on

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

  8. avatar
    wrote this comment on

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

Reply

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.
DjangoPythonBitcoinTuxDebianHTML5 badgeSaltStackUpset confused bugMoneyHackerUpset confused bugX.OrggitFirefoxWindowMakerBashIs it worth the time?i3 window managerWagtailContainerIrssiNginxSilenceUse a maskWorldInternet securityPianoFontGnuPGThunderbirdJenkinshome-assistant-logo