Project

General

Profile

HowToRedirectHttpToHttps » History » Revision 25

Revision 24 (gstrauss, 2020-05-20 05:25) → Revision 25/33 (gstrauss, 2020-05-28 01:16)

h1. How to redirect HTTP requests to HTTPS 


 h3. 

 h1. Example 1 - redirect everything 

 lighttpd 1.4.50 or later may use: 
 <pre> 
 $HTTP["scheme"] == "http" { 
             url.redirect = ("" => "https://${url.authority}${url.path}${qsa}") 
 } 
 </pre> 


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

 
 Earlier versions of lighttpd 1.4.x may use: 
 <pre> 
 $HTTP["scheme"] == "http" { 
     $SERVER["socket"] != "127.0.0.1:80" # capture vhost name with regex conditiona -> %0 in redirect pattern 
     # must be the most inner block to the redirect rule 
     $HTTP["host"] =~ ".*" { 
         $SERVER["socket"] != "[::1]:80" { 
             url.redirect = ("" (".*" => "https://${url.authority}${url.path}${qsa}") 
         } "https://%0$0") 
     } 
 } 
 </pre> 


 h3. redirect h2. Example 2 - specific url from http to https 

 lighttpd 1.4.50 or later may use: 
 <pre> 
 $HTTP["scheme"] == "http" { 
             url.redirect = ("^/phpmyadmin/" => "https://${url.authority}${url.path}${qsa}") 
 } 
 </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") 
     } 
 } 
 </pre> 


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


 h3. redirect everything from http to https 

 use: 
 <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 = (".*" ("^/phpmyadmin/.*" => "https://%0$0") 
     } 
 } 
 </pre> 


 h3. redirect h2. Example 3 - only for specific vhost and url from http to https 

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


 h2. Further stuff 

 Also works the other way round (https -> http) with if @$HTTP["scheme"] @if $HTTP["scheme"] == "https"@