AgmLauncher
AgmLauncher

Reputation: 7270

How to display 404 error in Lighttpd if HTTP host does not match?

I have Lighttpd installed on an embedded system with no external internet access. Due to the use of cookies, we want to ensure that people don't visit the site it's serving on anything other than a prescribed domain.

I want to make it so that if you visit any invalid domain (e.g. http://aasdflajsdf.com, it shows a 404 page with a link to the actual domain it accepts.

I've set this up: server.error-handler-404 = "/404.html"

But how do I "invoke" the 404 if the host doesn't match? I would prefer not to redirect to the 404 page, I just want to serve the contents of the 404 page at the address the user typed in.

Upvotes: 0

Views: 2382

Answers (1)

gstrauss
gstrauss

Reputation: 2404

The most flexible solution is to write a CGI script (or FastCGI or SCGI), which returns exactly the HTTP status code you want (404) with exactly the content that you want.

A different, partial solution will serve the contents of 404.html, but with a 200 OK status code:

$HTTP["host"] != "example.com" {
    url.rewrite-once = ( ".*" => "/404.html" )
}

You could instead rewrite to a non-existent URL since you have configured a 404 handler. However, this might add quite a bit of noise to your error log:

server.error-handler-404 = "/404.html"

$HTTP["host"] != "example.com" {
    url.rewrite-once = ( ".*" => "/nonexistent.html" )
}

For another alternative, you can specify a 404 Not Found status code with a redirect. I know you mentioned that you wanted to avoid a redirect, but you might try this, anyway, to see if it does what you want.

if $HTTP["host"] != "example.com" {
    url.redirect-code = 404
    url.redirect = ( ".*" => "http://example.com/" )
}

Upvotes: 0

Related Questions