How to use AWS Application Load Balancer to setup Multi-Site redirections?
In the following blog, I have explained how to do site redirection using S3 and CloudFront which works only for single site. In a multi-site scenario, creating a S3 bucket and form a CloudFront distribution for each bucket is a cumbersome process so I have come up with an enhancement, we can use Application load balancer (ALB) to setup multiple-site redirection using just one ALB.
What is site redirection? When we want to redirect a site “www.myolddomain.com” to “www.mynewdomain.com” then it is…medium.com
What is an Application Load Balancer?
Application load balancer is a Layer 7 load balancer which allows us to specify set of rules. Based on those rules it can either redirect or forward the incoming request to respective destination.
Create an AWS Application Load Balancer by going to EC2 management console.
You want HTTPS site redirection also then you can add HTTPS listener by clicking on Add listener. You also need to have an ACM certificate in the same region else you can’t create ALB with HTTPS listener.
You can create single certificate with multiple site domains from the AWS console. But if you use AWS CLI or API you can create and attach multiple certificates to HTTPS listener of the ALB . Please note: there is no option to attach multiple certificates to an ALB listener in AWS console.
I have chosen default VPC here but you can use a custom VPC also but make sure that it has at lease 2 subnets.
If you want HTTPS site redirection also then you can add HTTPS port: 443 whitelisting by clicking on Add Rule.
We won’t be using it but we need to give at least one target to create an ALB.
We don’t need to register any instances to the target for site redirection. So go to the Review page and then Click on Create.
Click on + icon next Rules (top-left) then click on Insert Rule.
Here we are setting up redirection for http://www.myolddomain.com to https://www.mynewdomain.com, if you want setup direction to a HTTP site then change it appropriately.
We can also use the above procedure for following commonly used redirects:
http://mynewdomain.com to http://www.mynewdomain.com or https://www.mynewdomain.com
http://www.mynewdomain.com to https://www.mynewdomain.com
We can also redirect particular path of one domain to other using Path condition as show in the above picture.
You can follow above steps to add more site redirection rules to HTTP listener.
To add HTTPS site redirection rules, click on View/edit rules for HTTP : 443 (HTTPS) listener in ALB Listeners tab and follow the same steps as HTTP listener.
Use the DNS (CNAME) of the ALB to configure the DNS settings of the sites for which you want the redirection to happen.
We have learnt what is an ALB and how to use it to setup redirection rules for multiple sites. Try this approach and let me know if you have faced any issues. You can also contact me if you have any other requirements which are not discussed here then I can do research and help you.