Search code examples
http-redirectsslhttpsconfiglighttpd

Redirect rule that keeps connection mode (HTTPS | HTTP) in Lighttpd


I have a redirect rule so every request will be forced to have www. before them, the problem is that if someone connects through https:// they are redirect to http:// because I don't know what command should I use to difference between them. THis is the rule:

var.mdomain = "mydomain.com"
var.mdomainregexredir = "^mydomain\.com$"

$HTTP["host"] =~ var.mdomainregexredir {
  server_name = var.mdomain 
  url.redirect = ( "^/(.*)" => "http://www." + var.mdomain + "/$1" )
}

If possible, I want to know if there's a way to use only one redirect rule, I mean, I don't like to add 2 url.redirect, one inside http and another inside https, I want it to be like this (Simplified):

url.redirect = ( "^/(.*)" => (isHttps?"https":"http") + "://www." + var.mdomain + "/$1" )

Solution

  • What you'll need is this:

    $HTTP["host"] =~ var.mdomainregexredir {
        $HTTP["scheme"] =~ ".*" { # catch http and https
            # %0 references the above matched pattern (http or https)
            # also changed to just catch the whole path and redirect using it with $0
            url.redirect = ( ".*" => "%0://www.varmdomain$0" )
        }
    }
    

    Unless you have a compelling reason, I'd be redirecting everything to https. You can do the https and www redirects all as one too.