Project

General

Profile

Redirect HTTP to HTTPS on non-standard ports

Added by chrisv over 14 years ago

Hi everyone,

I have the following in my lighttpd configuration to redirect from HTTP on port 8080 to HTTPS on port 4343:

server.port = 8080

$SERVER["socket"] == "[::]:4343" {
     server.use-ipv6 = "enable" 
     ssl.engine = "enable" 
     ...
}
else $HTTP["host"] =~ "(.*)" {
     url.redirect = ( "^/(.*)" => "https://%1/$1" )
}

Unfortunately, the redirect doesn't work exactly like I hoped. It redirects me from http://url:8080 to https://url:8080. I'm sure this is because something is missing in the regex on the $HTTP["host"], but I'm not sure exactly how to structure the regex so that it covers a few cases in the same express:

  1. Standard ports (80 -> 443)
  2. Odd ports (80 -> 4343, 8080 -> 443, 8080 -> 4343)

Any help would be greatly appreciated!

Thanks,
Chris


Replies (2)

RE: Redirect HTTP to HTTPS on non-standard ports - Added by deanpence over 14 years ago

Do you mean to listen on ports 80, 443, 8080, and 4343? Or just 8080 and 4343? If you want to listen on all four, from what I understand, you'd need several $SERVER["socket"] conditionals to force lighttpd to listen on all those ports:

$SERVER["socket"] == ":8080" {
  url.redirect = ( "^(.*)$" => "https://whatever:4343/$1" )
}
else $SERVER["socket"] == ":80" {
  url.redirect = ( "^(.*)$" => "https://whatever:4343/$1" )
}
else $SERVER["socket"] == ":443" {
  # You may need to duplicate your SSL configuration (ssl.engine = "enable", etc.) here.
  url.redirect = ( "^(.*)$" => "https://whatever:4343/$1" )
}
else $SERVER["socket"] == ":4343" {
  # Put your virtual host configuration here.
}

If you've already established (somehow) that the server is listening on all the ports you want it to listen on already, I'd try this:

$SERVER["socket"] == ":4343" {
  # Put your virtual host configuration here.
}
else $HTTP["host"] =~ "^(.+)(:[0-9]+)?$" {
  url.redirect = ( "^/(.*)$" => "https://%1:4343/$1" )
}

RE: Redirect HTTP to HTTPS on non-standard ports - Added by lightman about 12 years ago

I realize this is an old thread but I couldn't find the answer to this but finally figured it out. This will redirect any traffic on port 8080 to port 4343 on https

$SERVER["socket"] == ":4343" {
    ssl.engine = "enable" 
    ssl.pemfile = "/pathToPemFile/server.pem" 
}
else $SERVER["socket"] == ":8080" {
   $HTTP["host"] =~ "([^:/]+)" {
     url.redirect = ( "^/(.*)" => "https://%1:4343/$1" )
   }
}
    (1-2/2)