Redirect HTTP to HTTPS

From SolusVM

Jump to: navigation, search

This is a rough guide to redirecting HTTP traffic to HTTPS in lighttpd

DISCLAIMER: Backup any and all files before editing. This guide worked perfect for me but use it at your own risk. I'm not affiliated with SolusVM other than being a happy customer.

Make the following changes to /etc/lighttpd/lighttpd.conf

At the top of the file, add the following three lines for rewrite, redirect and alias:


server.modules           = ( 
                                "mod_access",
                                "mod_rewrite",
                                "mod_redirect",
                                "mod_alias",
                                "mod_fastcgi",
                                "mod_accesslog" )

At the bottom of the file, add this section:


$SERVER["socket"] == ":5353" {
  $HTTP["host"] =~ "vps.yourdomain.com" {
    url.redirect = ( "^/(.*)" => "<nowiki>https://vps.yourdomain.com:5656/$1</nowiki>" )
  }
}

If you also want to redirect your "regular" webtraffic (So that your users can enter vps.yourdomain.com in their browser) add this code instead of the above code:

$SERVER["socket"] == ":80" {
  $HTTP["host"] =~ "vps.yourdomain.com" {
    url.redirect = ( "^/(.*)" => "<nowiki>https://vps.yourdomain.com:5656/$1</nowiki>" )
  }
}
$SERVER["socket"] == ":5353" {
  $HTTP["host"] =~ "vps.yourdomain.com" {
    url.redirect = ( "^/(.*)" => "<nowiki>https://vps.yourdomain.com:5656/$1</nowiki>" )
  }
}

Make sure you change the URL's in the section above to your own!

Now go restart lighttpd again using the following command and test in your browser!

/etc/init.d/lighttpd restart

or

service lighttpd restart

External sources

  1. lighttpd mod_redirect
  2. How to redirect HTTP requests to HTTPS
Personal tools
Installation/Upgrading
Customization