views:

1796

answers:

4

Hi,

I'm playing about with lighttpd on a small virtual private server. I two domains pointing to the server. I am using the latest version of lighttpd and mod_evhost on Ubuntu 8.10.

  1. I'm trying to set up a rule such that if anyone requests domain.com or www.domain.com they get served from /webroot/domain.com/www/

  2. Similarly, if anyone requests sub.domain.com they get served from /webroot/domain.com/sub/

  3. If people requests fake.domain.com (where /webroot/domain.com/fake/ does not exist) I would like them served from /webroot/domain.com/www/

The third requirement isn't quite so important, I can deal with people requesting subdomains that don't exist being served from the server document root of /webroot/server.com/www/ even if they requested fake.domain.com

I've included the relevant parts of my lighttpd.conf file below:

server.document-root = "/webroot/server.com/www/"

// regex to match sub.domain.com
$HTTP["host"] =~ "\b[a-zA-Z]\w*\.\b[a-zA-Z]\w*\.\b[a-zA-Z]\w*" {
    evhost.path-pattern = "/webroot/%0/%3/"    
}

// regex to match domain.com    
$HTTP["host"] =~ "\b[a-zA-Z]\w*\.\b[a-zA-Z]\w*" {
    evhost.path-pattern = "/webroot/%0/www/"    
}

So where am I going wrong? At the moment, all requests to *.domain.com and domain.com are being served from /webroot/domain.com/www/

I'd appreciate any help you guys could offer and if I've left anything relevant out please tell me!

Cheers, Rob

+1  A: 

For your first one, matching domain.com and www.domain.com: ^\b([wW]{3}\.)?[\w\d]*\.com\b$, and for the second one, I am unsure if regex can determine if a subdomain/page exists, as it is for identifying strings of text of interest. Hopefully that will help you out a bit.

Anders
+2  A: 

Your regexes seem to be a bit overdone.

Here is what I would use:

// regex to match sub.domain.com
$HTTP["host"] =~ "^[^.]+\.[^.]+\.[^.]+$" {
    evhost.path-pattern = "/webroot/%0/%3/"    
}

// regex to match domain.com    
$HTTP["host"] =~ "^[^.]+\.[^.]+$" {
    evhost.path-pattern = "/webroot/%0/www/"    
}

where:

[^.]+ matches anything but a dot, 1..n times

To match only valid sub domains with fall back to "www", you can use this:

// default: route everything to "www"
$HTTP["host"] =~ "([^.]+\.)?domain\.com$" {
    evhost.path-pattern = "/webroot/%0/www/"
}

// specific regex overwrites "path-pattern" for valid sub-domains only
$HTTP["host"] =~ "^(valid1|valid2|sub)\.domain\.com$" {
    evhost.path-pattern = "/webroot/%0/%3/"    
}
Tomalak
Isn't it kind bad practice to make a regex that will match invalid URL characters?
Anders
It would be trivial to amend the regex to match only valid domains. I'll insert another bit of code to illustrate.
Tomalak
Apart from that: If this code runs, the requests have made it to your server. They'll very probably have no invalid characters in their host header.
Tomalak
A: 

@Anders, cheers for your input mate. You're right in that I will need to handle the fake subdomains outside of the mod_evhost environment.

@Tomalak, that worked perfectly for matching www.domain.com and domain.com to serve from /webroot/domain.com/www and it works for all subdomains that actually exists too (points sub.domain.com to /webroot/domain.com/sub/)

I'm getting 404 errors when I try to go to fake.domain.com but I can write a custom error page that reroutes *.domain.com 404 errors to www.domain.com

Thanks a million guys!

Rob Burke
Yup, take it easy.
Anders