3

I have two sub-domains which I want to redirect to the same directory:

$HTTP["host"] =~ "sub1\.example\.com$" {
    server.document-root = "/home/adam/html/sub_domain" 
}

$HTTP["host"] =~ "sub2\.example\.com$" {
    server.document-root = "/home/adam/html/sub_domain" 
}

Naturally, I've tried:

$HTTP["host"] =~ "sub1\.example\.com$" OR $HTTP["host"] =~ "sub2\.example\.com$"{
    server.document-root = "/home/adam/html/sub_domain" 
}

But got:

2011-03-14 10:19:30: (configfile.c.855) source: /etc/lighttpd/lighttpd.conf 
    line: 199 pos: 36 parser failed somehow near here: or

This failed with OR (upper case), or and even c-style ||.

Any idea how to avoid the awkward code repetition?

This question is a copy of an unanswered post I've published in the lighttpd forum.

Adam Matan
  • 13,194
  • 19
  • 55
  • 75

3 Answers3

3

Why not just...?

$HTTP["host"] =~ "^sub(1|2)\.example\.com$" {
    server.document-root = "/home/adam/html/sub_domain" 
}
Alicia
  • 765
  • 1
  • 5
  • 8
1

Alicia's answer is probably the best for this question, but if someone stumbles upon this for whom a regex fix doesn't work – like I did before – I want to quickly share which solution worked for me:

Lighttpd has no AND / OR logical operators, but it's possible to obtain the same behavior by nesting / chaining conditions.

Logical AND:

condition1 {
    condition2 {
        doSomething
    }
}

→ nested if blocks are equivalent to a logical AND

Logical OR:

condition1 {
    doSomething
}
condition2 {
    doSomething
}

→ chained if blocks are equivalent to a logical OR

if-then-elif-else construct:

condition1 {
    doSomething1  ← then block
}
else condition2 {
    doSomething2  ← else if block
}
else {
    doSomething3  ← else block
}

Taken from: https://doc.callmematthi.eu/lighttpd.html

Uwe Keim
  • 2,420
  • 5
  • 30
  • 47
S818
  • 111
  • 1
  • I'd add an `else` in the "logical OR" case too, else if both conditions are true it would "do something" twice. – lapo Sep 06 '22 at 20:18
0

Try -o for logical OR. Try -a for logical AND.

Also reference this page for some more operator commands in *nix.

report back and let us know if that works!

Split71
  • 548
  • 4
  • 9
  • Does not work: `2011-03-14 14:04:05: (configfile.c.855) source: lighttpd.conf line: 200 pos: 36 parser failed somehow near here: -o` – Adam Matan Mar 14 '11 at 14:04