Project

General

Profile

Actions

How to redirect HTTP requests to HTTPS

redirect everything

$HTTP["scheme"] == "http" {
            url.redirect = ("" => "https://${url.authority}${url.path}${qsa}")
}

redirect everything, but not for connections from IPv4 or IPv6 localhost

$HTTP["scheme"] == "http" {
    $SERVER["socket"] != "127.0.0.1:80" {
        $SERVER["socket"] != "[::1]:80" {
            url.redirect = ("" => "https://${url.authority}${url.path}${qsa}")
        }
    }
}

redirect specific url from http to https

$HTTP["scheme"] == "http" {
            url.redirect = ("^/phpmyadmin/" => "https://${url.authority}${url.path}${qsa}")
}

redirect specific vhost and url from http to https

$HTTP["scheme"] == "http" {
    $HTTP["host"] == "sth.example.com" {
            url.redirect = ("^/phpmyadmin/.*" => "https://sth.example.com$0")
    }
}

Earlier versions of lighttpd 1.4.x before lighttpd 1.4.50 may replace the url.redirects above with a combination of regexes:

redirect everything from http to https

$HTTP["scheme"] == "http" {
    # capture vhost name with regex conditiona -> %0 in redirect pattern
    # must be the most inner block to the redirect rule
    $HTTP["host"] =~ ".*" {
        url.redirect = (".*" => "https://%0$0")
    }
}

redirect specific url from http to https

$HTTP["scheme"] == "http" {
    $HTTP["host"] =~ ".*" {
        url.redirect = ("^/phpmyadmin/.*" => "https://%0$0")
    }
}

Updated by gstrauss about 4 hours ago · 26 revisions