Redirecting HTTP to HTTPS

Learn to create an automatic redirect from HTTP to HTTPS to ensure users utilize encrypted communication.

There is just one final step to complete our deployment, and that is to redirect HTTP traffic to HTTPS. This automatic redirection is a security feature and protects our users from connecting over an unsecured connection. We do this so that every time they accidentally try to connect via HTTP, they will be redirected to use the secure HTTPS mode instead.

Setting up this redirection is very easy. We just need to remove our HTTP listener from our load balancer and add a new HTTP redirect listener instead. The configuration will be very similar to our current HTTP listener, but instead of the forward action, we will use redirect.

Delete the current HTTP listener

Before we create the new HTTP redirect listener, we first should delete the old HTTP listener. To do so, we need to retrieve the listener ARN and pass it to the delete-listener CLI command.

To get the listener ARN, we can use the describe-listeners command and pass the load balancer ARN of our wordpress-lb and then filter the output via the --query parameter. We can filter the listeners by looking for the listener with the HTTP protocol (the query parameter would be Listeners[?Protocol=='HTTP'].ListenerArn).

The command below gets the listener ARN.

Get hands-on with 1200+ tech skills courses.