I want to use an Application Load Balancer to redirect traffic from one domain name to another.
Short description
To use an Application Load Balancer to redirect one domain to another, create a listener rule in the Amazon Elastic Compute Cloud (Amazon EC2) console. Application Load Balancers support domain-to-domain and HTTP-to-HTTPS redirection.
It's best practice to use an Application Load Balancer instead of Amazon Simple Storage Service (Amazon S3) for this configuration.
Resolution
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.
To create a listener rule in the Amazon EC2 console, complete the following steps:
- Open the Amazon EC2 console.
- On the navigation pane, under Load Balancing, choose Load Balancers.
- Select your load balancer, and then choose Listeners.
- Choose View/edit rules for the load balancer listener that you want to use.
- Choose the Add rule icon (the plus sign).
- Choose Insert Rule.
- Choose Add condition.
- In the conditions section (IF), choose Add condition. Then, complete the following steps:
Choose Host header, and then enter your hostname (for example, example.com).
To save, choose the checkmark icon.
- In the actions section (THEN), choose Add action. Then, complete the following steps:
Choose Redirect to.
Specify the protocol and port.
Change Original host, path, query to Custom host, path, query.
For Host, enter example2.com.
For Path and Query, keep the default values (unless your use case requires you to change them).
Set the Response to HTTP 301 "Permanently moved" or HTTP 302 "Found".
To save, choose the checkmark icon.
The THEN section now appears as the following:
Redirect to https://example2.com:443/#{path}?#{query}
Status code: HTTP_301
- Choose Save.
Note: If both domains point to the same Application Load Balancer, then complete one of the following tasks:
- Have separate certificates for both domains.
- Use a Subject Alternative Name (SAN) certificate to validate the domains.
To confirm that the redirect is working, complete the following steps:
-
In the AWS CLI, use the following curl function:
curl
-Iv https://example.com -L
* Rebuilt URL to: https://example.com/
. . .
* Connected to example.com (1.2.3.4) port 443 (#0)
<SSL handshake>
> Host: example.com. ------> Host name is example.com
> User-Agent: curl/7.61.1
>
Accept: */*>
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
< HTTP/2 301 ------> ALB does redirection
< server: awselb/2.0
< date: Fri, 06 Mar 2020 09:18:33 GMT
< content-type: text/html
< content-length: 150
< location: https://example2.com:443/. ----> redirected to "example2.com"
<
* Issue another request to this URL: 'https://example2.com:443/'. ------> Curl initiates another request that is to example2.com
* Trying 34.195.219.169...
* TCP_NODELAY set
<SSL handshake>
> Host: example2.com. ------> Host name has changed to example2.com
> User-Agent: curl/7.61.1
> Accept: */*
>
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
<
HTTP/2 200 ----> We got a response
-
In your internet browser, enter example.com and confirm that it redirects to example2.com.
Note: Application Load Balancer supports only 301 and 302 redirects. These redirects allow the client to change the HTTP method from POST to GET in subsequent requests. If you need a 307 redirect, then the redirect must come through the target application.
Related information
How do I redirect an apex domain to its subdomain or any other domain using Amazon S3 and Amazon Route 53?
Application Load Balancers now support multiple TLS certificates with smart selection using SNI