Lighttpd rewrite subdomains to subfolders

domain-name-systemlighttpdmod-rewritesubdomainwildcard-subdomain

I have a server (itself on a subdomain) with wildcard DNS pointing to it. The server is running Lighttpd and PHP.

I need requests to http://any-subdomain.server.example.com to rewrite to http://server.example.com/site/any-subdomain and be served from there. It has to be completely transparent to the user.

Here's my relevant config:

$HTTP["host"] =~ "^(.*)\.server\.example\.com$" {
    url.rewrite-once = ( "^(.*)" => "/site/%1/$1" )
}

It seems to work except for subfolders, which get redirected to subdomain.server.example.com/site/subdomain/subfolder and then 404's.

Best Answer

url.rewrite-once changes the URL path (and querystring); your rewrite rule looks like you're trying to rewrite to a folder on your disk.

You cannot "rewrite" the host, but you can handle two hostnames with the same physical file mapping (and fastcgi backends and ...); for example you could copy the server.document-root setting from the other vhost, or just make the other vhost conditional match both hostnames.

If you have "catch-all" default vhost blocks keep in mind that for each config option the last active block setting it overwrites all others (use the -p option to see the internal order after evaluation of += and so on), so start with the default vhost(s).

Just for the record: the "relevant" config includes at least the parts for both vhosts, not just the second :)