lighttpd configuration to redirect subdomains

For my NeoInvoice project, all requests to the server's sub-domains are sent right to the main server. This means someone can go to www.neoinvoice.com, ex1.neoinvoice.com, etc. Search engines don't like to see duplicate content on web servers, cookies being saved on one sub-domain aren't accessible to another, there are all sorts of problems with this. Down the road we do plan on setting up sub-domains for every company (in which case the application will do redirects, not the server), but in the mean time we want to disable all sub domains (except for www).

Adding this rule to your lighttpd configuration file will redirect all sub-domains to the www. sub-domain. People going to the main domain will also be redirected to the www. sub-domain.

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

Thomas has contributed to dozens of enterprise Node.js services and has worked for a company dedicated to securing Node.js. He has spoken at several conferences on Node.js and JavaScript, published a book on microservices, and is an organizer of Nodeschool SF.