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 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.

Steps

Create an AWS Application Load Balancer by going to EC2 management console.

Select Load Balancers from EC2 Console
Click on Create Load Balancer
Click on Create in Application Load Balancer panel
Choose internet-facing

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.

Only required incase you want to redirect a HTTPS website
Select at least 2 subnets
Select a certificate matching to the site domain names

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.

Click on Create a new security group

If you want HTTPS site redirection also then you can add HTTPS port: 443 whitelisting by clicking on Add Rule.

Only required incase you want to redirect a HTTPS website
Create a Target and Click Next

We won’t be using it but we need to give at least one target to create an ALB.

Click on Next to Review

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.

Navigate to Listeners Tab and Click on View/edit rules
Click on Insert Rule

Click on + icon next Rules (top-left) then click on Insert Rule.

Click on Add condition and then on Host is…
Enter your old domain in IF section and select Redirect to… in THEN section
Enter details of the new domain in THEN section

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

Path based redirection

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.

Click on HTTP : 443-View/edit rules to add HTTPS site redirections

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 CNAME of the ALB to configure your site(s) DNS settings

Use the DNS (CNAME) of the ALB to configure the DNS settings of the sites for which you want the redirection to happen.

Conclusion

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.