Matching domains with regex for lighttpd mod_evhost (www.domain.com / domain.com / sub.domain.com)

StackOverflow https://stackoverflow.com/questions/261904

  •  06-07-2019
  •  | 
  •  

Question

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

Was it helpful?

Solution

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/"    
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top