I recently moved my WordPress blog over to AWS. To ensure I could easily scale my blog and also to make it easy to add SSL I decided to create an Elastic Load Balancer, even though most of the time my website runs on a single instance.
An elastic load balancer makes it really simple to set up and use the included certificate that AWS provides for your custom domains. I will write a blog about that later on in the week.
After setting up my blog and my TLS I noticed that the two domains I have (thebeebs.co.uk and thebeebs.net) were working great when people visited using HTTPS in their browser. However, if they used HTTP then their browser would give a Not Secure warning.
This was simply because I had forgotten to add a redirect for traffic coming from HTTP. I would usually handle this at the server level, or perhaps at the application level, but since I was already using HTTPS I decided to deal with it at the load balancer level.
My load balancer has two listeners. One for port 80 and one for port 443.
Currently, my port 80 listener has a rule that forwards all traffic to my blog (This is an auto-scaling group of EC2 instances that run WordPress)
What I need to do is change this behavior so that all traffic on port 80 is redirected to https and port 443.
To do this I click on View/edit rules on the Rules column of the load balancer.
I edit the rule and start by deleting the existing THEN action of my rule. I then add a new redirect action that moves the redirects the action to the https address for my load balancer.
That’s now my HTTP redirecting to HTTPS issue sorted out.
There is one extra thing I’d also like to take care of and that is my Canonical Url. For a long time, I used the domain thebeebs.co.uk and I still have lots of traffic that come through that domain. However, going forward I would like to use the thebeebs.net domain.
Therefore I want the rule to redirect traffic from thebeebs.co.uk through to the thebeebs.net.
To do this I change the action to use a custom host, path, query and change the host the thebeebs.net. Now all domains that are hooked up to this load balancer will now redirect to the thebeebs.net.
Now I have one last thing to do. Those that come in on the URL https://thebeebs.co.uk will not be affected by the rule above I also need to make sure that the 443 rule also redirects this domain.
Now I am nearly done, just one last edge case. What if someone comes in on https://www.thebeebs.co.uk or https://www.thebeebs.net I need to also make sure I fix those ones.
Hopefully, now all my traffic is redirected to the correct port and the correct domain name no matter where they have come from.