I want to use an Application Load Balancer to redirect one domain to another domain.
Short description
The Application Load Balancer service supports redirection of domain names as well as redirection from HTTP to HTTPS. If you have a domain that points to an Application Load Balancer, then use the Application Load Balancer to configure the domain.
Note: It's a best practice to use the Application Load Balancer to configure the domain instead of Amazon Simple Storage Service (Amazon S3).
Resolution
Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you're using the most recent AWS CLI version.
If you use an Application Load Balancer as part of your configuration, then you can use it to redirect one domain to another:
- Open the Amazon Elastic Compute Cloud (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, as your use case requires.
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:
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:
1. 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
2. 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 Server Name Indication (SNI)