Use mod_redirect with lighttpd

Enable mod_redirect

server.modules += ( "mod_redirect" )

Force your domain with www.

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

Force your domain without www.

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

Redirect HTTP requests to HTTPS

$SERVER["socket"] == ":80" {
    $HTTP["host"] =~ "(.*)" {
        url.redirect = ( "^/(.*)" => "https://%1/$1" )
    }
}

Redirect an old domain to your new domain

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

Redirect a url that can be matched by a mod_rewrite rule

Lets say that you want to redirect http://www.domain.com/party to http://blog.domain.com/party while you have a mod_rewrite rule on www.domain.com that redirect all URLs to a controller (“^(.*)$” => “/index.php$1”).

Your redirect will not work because mod_rewrite always execute before redirect rules. To bypass this, you need to use the $0 as the rule target to pass URLs through unmangled.

e.g.

url.redirect = ( "^/party(.*)" => "http://blog.domain.com/party$1" )
url.rewrite-once = ( "^/party" => "$0" )

tested with lighttpd 1.4.x

 
  • Thanks for this, everyone else is over-complicating things on this issue.

  • heidar

    What Rick said. I was stuck for about an hour trying to find out how to do this. Thanks a lot!