Using Application Load Balancers to Handle Multiple Domain Redirects

I hope everyone has been following safety measures and staying inside to be healthy. The time bygone and the present have been unfamiliar in long stretches, forcing us all to find ways to peck our mind in different areas.
Since you are here, let me share a problem where I applied a similar idea.
In my time helping develop high-performance infrastructure and services at DLT Labs™, I’ve learned a lot about the role of load balancers while making them scalable.
One of the cloud providers we work with is Amazon Web Services, and today I thought I’d talk a little about what they call an Application Load Balancer (ALB), and what is needed to set one up.
Amidst all ongoing activities, one day my team witnessed a bottleneck caused by a requirement for multiple domains to be redirected to a single domain. On looking out for possible solutions, I came across a few. Naturally, as solutions go, they presented certain limitations too.
Magically, an Application Load Balancer can help provide a one-stop solution to clearly sweep-off this problem. Thereafter, you can go and explore more to take advantage of its different use cases in various areas. Now, let’s get familiar with the problem, related approach, and its solution.
#The problem
Here is some context before diving right into the solution:
For example, let's say there are three domains: “testprac1.example.com”, “testprac2.example.com”, and “testprac3.example.com” and all these domains are required to serve HTTP/HTTPS requests.
Requests of any either nature — HTTP or HTTPS for each of the domains above, must be involuted into the same request — i.e., a request to “testprac2.example.com” should be redirected to “testprac1.example.com”. Likewise, a request to “testprac3.example.com” should be redirected to “testprac1.example.com”.
In case you are hosting static data, Amazon S3 and Amazon Route53 would come into the picture, or you can create several domains for each record to be served.
This isn’t the simplest solution, as it isn’t what we call a feels-right-sort-of-solution!
Undoubtedly, I too felt the same, and ultimately found a gem of a source. What I found, I am going to put all of that for you in the following solution.
The solution
What we are going to use here, is a ‘Layer 7 load balancer’. Layer 7 is a term for the application layer, where the human-computer interaction happens, from the widely-used OSI model of computer systems.
This will allow us to dictate a set of rules. These rules can make it either redirect or forward the incoming request(s) to the corresponding destination(s) using a mapping form of mechanism.
We will use ALB which provides the capability to redirect requests from one domain to the newer domain that we intend to serve.
>> Finding the ALB option in AWS
- To start preparing an Application Load Balancer, we will browse through the services on the AWS console and select EC2.

- In EC2, using the service navigation pane, find Load Balancing, and select Load Balancers.

- Click on Create Load Balancer as displayed in the picture, following which a passage will be opened with several steps to describe and create a Load Balancer.

- Select Application Load Balancer here

Note — For the ease of follow-up, further detail of setup, configurations, etc have been described via the next set of steps provided.
>> Creating the Application Load Balancer
1. Building the Application Load Balancer requires some basic information
- LB Name
- Internet-facing/internal*
- Ipv4 address type
*Note: For the internet-facing scheme, public subnet(s) would be used, and for internal LB, private subnet(s) would be used.

Choose the listeners here, followed by selecting the Availability Zones and add-on services that can be opted into as per your choice.

*Note: For the internet-facing scheme, public subnet(s) would be used, and for internal LB, private subnet(s) would be used.
Click Next on the console.
2. Select a certificate and the relevant security policy

Click Next on the console.
3. Assign a security group
A security group is a set of firewall rules that control the traffic to your load balancer. You can add rules to allow specific traffic to reach your load balancer. Firstly, you should decide whether to create a new security group or find an existing one.

Click Next on the console
4. Select optimal health check settings also for your target
Your load balancer works by routing requests to the targets in the target group specified using the protocol and port that we specify. Here we can select optimal health check settings also for your target.
Target groups leverage added ports, protocol in the relevant VPC with the specified instance.
Click Next on the console
5. Register your target instance
The target instance is registered here using the instance id. We will need to ensure the instance is active to register it.
Click Next on the console
6. Verify your details
On the review window, check the ports, security policy, and scheme to make sure the details are correct.
Click on Create.
We have created the load balancer with the steps taken so far, Superb!
>> Fine-tuning our load balancer to handle multiple domain redirects
We are nearly there. Now, is the redirection part where we will be using rules to forward/redirect requests. This is why we set up the ALB, so let’s prepare!
*Note: Assuming you have chosen HTTP protocol while creating ALB, the steps will incorporate the HTTP listener.
1. Setting up the Listener
First, go to the created ALB, then click on the Listeners tab:

A tabular format of listeners contains details in multiple columns. Here you will see the ‘Rules’ column.
2. Set rules for HTTP listener

For the HTTP listener, click View/edit rules. Initially, it should look like this:

Please note — Rules use the IF-THEN construct, which corresponds to Conditions-Actions
Now, let’s begin adding conditions and actions.
3. Configure Listener, HTTP, View/edit & Insert rules

Go to the ‘Add Rule’ button and click on ‘Insert Rule’.
In the IF column, click on the Add condition then select Host Header. You should see this:

Set value for Host header to “testprac2.example.com”, and click the blue tick.
In the IF column, click again on Add condition, then select Path. Set the path to ‘*’, like so:

Note — you can customize the Path according to the use-case.
Now, for the Action column, select Redirect to.
Provide your choice here for protocol and port number. In the drop-down menu, select ‘Custom host, path, query(optional)’ and provide the new domain and path as shown below with the code ‘301-permanently moved’.

It should look like this once all the correct details are in place:

To better understand what we just did, this last step redirects an HTTP request for “testprac2.example.com” to “testprac1.example.com” in the form of an HTTPS request.
For the other domains, we can repeat the steps in point 3, with relevant Host, Headers, etc.
It should finally look like this:

So far, the redirection achieved is for HTTP port 80. Similarly, you can add listeners using HTTP/HTTPS. All you need to do is re-iterate through step 1 to step 3 in this section, with the relevant details.
Good work! We have completed the setup of the Application Load Balancer, and your motive has been achieved to serve the use case — to handle multiple domain redirects. You can customize the ALB further, to your taste and requirement.
Keep learning and sharing!
Amazon, Amazon Web Services, AWS, Amazon S3, Amazon Route 53, and Amazon EC2 are trademarks of Amazon.com, Inc. or its affiliates in the United States and/or other countries. DLT Labs is a trademark of DLT Global, Inc.
Author — Aman Gupta, DLT Labs™
About the Author: Aman is part of the core DevOps & Infrastructure team at DLT Labs and is skilled in AWS, C++, DS-A, and open-source tools. He loves playing Badminton and enjoys e-gaming.