« Adding SSL support to Squid package on Ubuntu | Main | Lighttpd, virtual hosts, alternative ports »
Lighttpd, virtual hosts, and wildcard domains
By Matt | May 29, 2009
So we’re setting up mirocommunity.com, and I don’t want to be hassled continously to create new hostnames in DNS.
To avoid that, it’s a simple wildcard entry like this in the appropriate named database:
*.mirocommunity.com. IN CNAME mirocommunity.com.
Which directs everything to our server.
Now our server hosts multiple sites via host entries, so we can’t use a simple negation like this:
$HTTP["host"] !~ “^(www|medfield)\.mirocommunity\.(com|net|org)($|:8080$)” {
url.redirect = (
“^(.*)$” => “http://www.mirocommunity.com$1″,
)
}
Note the negation by using !~ instead of =~. That would work if all we had was mirocommunity sites to host, but when hitting another site on the server like www7.getmiro.com it would read it as not being www or medfield dot mirocommunity, and thus drop you to www.mirocommunity.com. For the curious, the 8080 part of the url parsing is a bypass of the Squid proxies on ports 80 and 443.
Anything that doesn’t match a virtual host or alias on our server gets dropped by default to /var/www.
There lies the simple solution — put an index.php file there that does the redirect work:
<?php
// Hostnames that aren’t matched in Lighttpd get dropped here
// by default.
// This script removes the hostname(s) and drops them to
// www.[domain].[tld]
// 29 May 2009 MRK
$split_host = split(“\.”, $_SERVER[HTTP_HOST]);
$domain = count($split_host) – 2;
$tld = count($split_host) – 1;
$new_host = “http://www.$split_host[$domain].$split_host[$tld]“;
// echo “$new_host”;
header(“Location: $new_host”);
exit;
?>
The split command splits the $_SERVER[HTTP_HOST] variable at each period, and put it’s contents less the periods into an array called $split_host.
The count($split_host) determines how many members we have in the $split_host array. We know we always want the last (the top level domain — .com, etc) and second to last (the domain — mirocommunity, etc). Since arrays start at 0, we simply count -1 for the tld and -2 for the domain.
By adding the count logic, we can handle domains like brooklyn.newyork.mirocommunity.com which have more then one hostname before the domain and tld.
$new_host then forms the URL we want to catch wildcard hostnames that haven’t been configured yet. It’s simply the www.domain.tld form. That’s fed to a http header which causes the user’s browser to redirect to the default website we want.
So as of today, while newyork.mirocommunity.com and brooklyn.newyork.mirocommunity.com have no virtual hosts, you do arrive successfully at www.mirocommunity.com.
Our developers can activate those hostnames simply by adding an entry in the appropriate lighttpd conf file and reload lighttpd — no need to contact the sysadmin to go make an entry in our DNS system for each new city added.
Topics: Linux, Sysadmin Tools, Web Hosting Tools | No Comments »
Comments
You must be logged in to post a comment.