Lighttpd redirect from www.domain.com to domain.com

I need to permanently redirect all www.domain.com to domain.com in Lighttpd.

Here's relevant part of my lighttpd.conf:

$HTTP["host"] =~ "^www\.domain\.com$" {
    url.redirect = (
        "^/(.*)" => "http://domain.com/$1"
    )
}

$HTTP["host"] =~ "(^|\.)domain\.com$" {
     ...
     url.rewrite-once = (
         ...
         "^(/.*)$" => "/mysite.fcgi$1",
     )
}

Problem is that all URLs like http://www.domain.com/blah/ get redirected to http://domain.com/mysite.fcgi/blah/

I need the "mysite.fcgi" rewrite rule since it sends the request to fastcgi instance.

How do I redirect properly?


If you need to redirect all requests made to www.domain.com/ regardless of the request path, just drop the $1 at the end of the third line so it looks like this:

$HTTP["host"] =~ "^www\.domain\.com$" {
    url.redirect = (
        "" => "http://domain.com/"
    )
}

$1 is substituted with the first match of the ^/(.*) regular expression, which is, in your case, is everything after the first slash.