Skip to content

How to redirect HTTP traffic to HTTPS

Lloyd Brookes edited this page Jun 24, 2019 · 8 revisions

This is required to satisfy the Lighthouse audit.

Summary

HTTP and HTTPS are two separate services runing on separate ports (80 and 443). Therefore, we need to launch two servers - one catching rogue HTTP traffic on port 80, the other serving the main application via HTTPS on port 443.

The HTTP server on port 80 should do nothing but redirect each request to the equivalent path via HTTPS.

Catch HTTP traffic

First, install lws-redirect locally.

$ npm install --save-dev lws-redirect

Launch a server on port 80 with a single redirect rule: redirect everything to HTTPS.

$ ws --stack redirect  --port 80 --redirect 'http -> https'

Test. Notice the Location header returned is https.

$ curl -I http://127.0.0.1/
HTTP/1.1 302 Found
Location: https://127.0.0.1/
Content-Type: text/html; charset=utf-8
Content-Length: 67
Date: Sun, 09 Jun 2019 16:53:38 GMT
Connection: keep-alive
Clone this wiki locally