HowToRedirectHttpToHttps » History » Revision 32
Revision 31 (gstrauss, 2021-07-17 23:33) → Revision 32/33 (gstrauss, 2021-08-26 15:54)
h1. How to redirect HTTP requests to HTTPS Further details and examples can be found in [[Docs_ModRedirect|mod_redirect]] h3. redirect everything <pre> $HTTP["scheme"] == "http" { url.redirect = ("" => "https://${url.authority}${url.path}${qsa}") url.redirect-code = 308 } </pre> h3. redirect everything, but not for connections from IPv4 or IPv6 localhost <pre> $HTTP["scheme"] == "http" { $HTTP["remote-ip"] != "127.0.0.1" { $HTTP["remote-ip"] != "[::1]" { url.redirect = ("" => "https://${url.authority}${url.path}${qsa}") url.redirect-code = 308 } } } </pre> </pre> h3. redirect specific url from http to https <pre> $HTTP["scheme"] == "http" { url.redirect = ("^/phpmyadmin/" => "https://${url.authority}${url.path}${qsa}") url.redirect-code = 308 } </pre> h3. redirect specific vhost and url from http to https <pre> $HTTP["scheme"] == "http" { $HTTP["host"] == "sth.example.com" { url.redirect = ("^/phpmyadmin/.*" => "https://sth.example.com$0") url.redirect-code = 308 } } </pre> h3. redirect everything from a non-standard port (i.e. not 80) from http to https https://stackoverflow.com/questions/62109311/lighttpd-redirect-from-custom-http-port-81-to-https-port-443 <pre> $HTTP["scheme"] == "http" { $HTTP["host"] =~ "^(.*?)(:\d+)?$" { ## %1 contains hostname without port url.redirect = ("" => "https://%1${url.path}${qsa}") url.redirect-code = 308 } } </pre> h3. Earlier versions of lighttpd 1.4.x before lighttpd 1.4.50 may replace the url.redirects above with a combination of regexes: h3. redirect everything from http to https <pre> $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") url.redirect-code = 308 } } </pre> h3. redirect specific url from http to https <pre> $HTTP["scheme"] == "http" { $HTTP["host"] =~ ".*" { url.redirect = ("^/phpmyadmin/.*" => "https://%0$0") url.redirect-code = 308 } } </pre>