How can I redirect one domain to another domain using an Application Load Balancer?

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 it’s a best practice to configure redirection using the Application Load Balancer rather than 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’re using an Application Load Balancer as part of your configuration, you can use it to redirect one domain to another:

  1. Open the Amazon Elastic Compute Cloud (Amazon EC2) console.
  2. On the navigation pane, choose Load Balancers under Load Balancing.
  3. Select your load balancer, and then choose Listeners.
  4. Choose the Rules tab and “Manage Rules” button on the load balancer listener that you want to use.
  5. Choose the Add rule icon (the plus sign).
  6. Choose Insert Rule.
  7. Choose Add condition.
  8. In the conditions section (IF), choose Add condition.
    Choose Host header, and then enter your hostname (for example, example.com).
    To save, choose the checkmark icon.
  9. In the actions section (THEN), choose Add action.
    Choose Redirect to.
    Specify the protocol and port, as required by your use case.
    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
  10. Choose Save.

Note: If both domains point to the same Application Load Balancer, be sure that you:

  • Have separate certificates for both domains, OR
  • Use a Subject Alternative Name (SAN) certificate to validate the domains

To confirm that the redirect is working:

1.    In the AWS CLI, use the curl function.

curl
    -Iv https://example1.com -L
* Rebuilt URL to: https://example1.com/
. . . 
* Connected to example1.com (1.2.3.4) port 443 (#0)
 
> Host: example1.com.   ———> Host name is example1.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
 
> 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 example1.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 a 307 redirect is needed, then the redirect must come through the target application.