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.

  • Do you want the user to be redirected (have their address bar change to the new address) or for them to be transparently served the content from the other domain? Can you share the relevant current configuration? Jun 13, 2014 at 22:17
  • @ShaneMadden It has to be completely transparent to the user. Jun 13, 2014 at 22:20
  • So, just to confirm: you want them to be served the content from docroot-of-server.example.com/site/subdomain without their address bar changing, right? You'll need to provide at least some of your current config to be able to have an answer fit into it, otherwise all we'll be able to give you is rough guidelines. Jun 13, 2014 at 22:23
  • @ShaneMadden I added relevant config. And yes, I have a site folder where the subdomains should be served from. Users would have a subdomain that is exactly the same as their site subfolder. Jun 14, 2014 at 2:30
  • Can you clarify what you mean about the problems you're having with subdirectories? Is subdomain.server.example.com/site/subdomain/subfolder a problem because it's doing the subdomain translation twice? And where are you seeing that URL? Jun 14, 2014 at 19:14

2 Answers 2


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 :)


I found a better way then using rewrites. Instead, I used mod_simple_vhost:

simple-vhost.server-root = "/var/www/site/"
simple-vhost.default-host = "myquick.club"
simple-vhost.document-root = "www"

This is a lot simpler and easier than using mod_rewrite in this case. It was a chore to find though, I hope this helps others in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.