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.

3 Answers 3

3

Why not just...?

$HTTP["host"] =~ "^sub(1|2)\.example\.com$" {
    server.document-root = "/home/adam/html/sub_domain" 
}
1
  • Note: This method does not work with $SERVER["socket"]. Apr 30, 2015 at 14:16
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

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 6, 2022 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!

1
  • 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, 2011 at 14:04

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.