How can I redirect one domain to another in Route 53?

Last updated: 2021-05-27

I need to redirect my client from one domain to another domain in Amazon Route 53. How can I do this?

Short description

There are three common scenarios for configuring domain redirection:

1.    Redirecting from one domain to another domain. For example:
        Apex domain (example.com) to apex domain (anydomain.com)
        Apex domain (example.com) to subdomain (www.anydomain.com), including www.example.com
        Subdomain (www.example.com) to apex domain (anydomain.com), including example.com
        Subdomain (www.example.com) to subdomain (www.anydomain.com), including anysubdomain.example.com
2.    Redirecting a domain from HTTP to HTTPS. For example:
        Apex domain (http://example.com) to apex domain (https://anydomain.com), including https://example.com
        Apex domain (http://example.com) to subdomain (https://www.anydomain.com), including https://www.example.com
        Subdomain (http://www.example.com) to apex domain (https://anydomain.com), including https://example.com
        Subdomain (http://www.example.com) to subdomain (https://www.anydomain.com), including https://anysubdomain.example.com
3.    Redirecting to a different URI path (path forwarding). For example:
        example.com, example.com/<path>, www.example.com, or www.example.com/<path> to
        example.com/<newpath>, www.example.com/<newpath>, anydomain.com/<newpath>, or anysubdomain.anydomain.com/<newpath>

Resolution

Redirecting from one domain to another domain using a CNAME record

In this scenario, create a CNAME record in your Route 53 hosted zone to configure DNS redirection.

For redirection from a non-apex domain to another domain type, configure your CNAME record using one of the following formats (as appropriate for your use case):

  • www.example.com CNAME anysubdomain.example.com or www.anydomain.com
  • www.example.com CNAME example.com or anydomain.com

Redirecting to a different URI path or from HTTP to HTTPS using Amazon S3 and CloudFront

These two common redirection scenarios can't be achieved through simple DNS redirection with CNAME records. In these scenarios, you must configure redirection on the application side. You can use Amazon CloudFront, Amazon Simple Storage Service (Amazon S3) static web hosting, and Route 53 to redirect requests.
Note: Amazon S3 static web hosting supports only the HTTP protocol. You must use a CloudFront distribution to perform redirection from HTTP to HTTPS.

Prerequisites

To redirect your domain in this scenario, you need:

  • A hosted zone for the domain (example.com) hosted in Route 53
  • Permissions to add resource records to the hosted zone for the domain (example.com)
  • Permissions to create an Amazon S3 bucket
  • Permissions to create CloudFront distributions
  • AWS Certificate Manager (ACM) certificates for your domain names (example.com and www.example.com)

Amazon S3 configuration

  1. Open the Amazon S3 console.
  2. Create an S3 bucket with a global unique name.
  3. Select the bucket you just created, and then choose Properties.
  4. For Static website hosting, choose Edit.
  5. For Hosting type, choose Redirect requests for an object.
  6. Enter the Host name (the new redirected location domain.com or domain.com/xxx/xxx), and then choose the protocol for the new domain.
  7. Choose Save.
  8. Make a note of the bucket's endpoint (example.com.s3-website-us-east-1.amazonaws.com). You'll use this information to configure the origin domain name for CloudFront in the following task.

CloudFront configuration

  1. Open the CloudFront console.
  2. Choose Create Distribution.
  3. For Web, choose Get Started.
  4. For Origin Domain Name, enter the S3 bucket's website endpoint that you noted in the previous task.
  5. For Viewer Protocol Policy, choose Redirect HTTP to HTTPS.
  6. Configure the remaining CloudFront settings per your requirements.
  7. If you aren't using a custom domain with CloudFront, then choose Create Distribution to complete the process.
    If you're using a custom domain, then complete these additional steps before creating the distribution:
    For Alternate Domain Names (CNAMEs), enter your custom domain (example.com).
    For SSL Certificate, choose Custom SSL Certificate. Then, choose the custom SSL certificate to assign to the distribution.
    Note: For more information on installing a certificate, see How do I configure my CloudFront distribution to use an SSL/TLS certificate?
    Choose Create Distribution.

Route 53 configuration

  1. Open the Route 53 console.
  2. Select the hosted zone (example.com).
  3. Create a resource record for example.com:
    For Record Type, choose A – IPv4 address.
    For Alias, choose Yes.
    For Alias Target, select the blank value field. From the dropdown, select the domain name of the CloudFront distribution you created in the previous task.
  4. Configure the remaining settings per your requirements, and then choose Create.

Redirecting domain names or redirecting from HTTP to HTTPS using an Application Load Balancer

The Application Load Balancer service supports redirection of domain names and redirection from HTTP to HTTPS. If you have a domain that points to an Application Load Balancer, it's a best practice to configure redirection using the Application Load Balancer rather than Amazon S3.

If you're using an Application Load Balancer as part of your configuration, then you can use the service to redirect one domain to another by completing the following steps.

Configure the Application Load Balancer

  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 View/edit rules for 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.
  9. Choose Host header, and then enter your hostname (for example, example.com).
  10. To save, choose the checkmark icon.
  11. In the actions section (THEN), choose Add action.
  12. Choose Redirect to.
  13. Specify the protocol and port, as required by your use case.
  14. Change Original host, path, query to Custom host, path, query.
  15. For Host, enter example2.com.
  16. For Path and Query, keep the default values (unless your use case requires you to change them).
  17. Set the Response to HTTP 301 "Permanently moved" or HTTP 302 "Found".
  18. To save, choose the checkmark icon.
    The THEN section displays:
    Redirect to https://example2.com:443/#{path}?#{query}
    Status code: HTTP_301
  19. 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

Confirm that the redirect is working as intended

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.

1.    In the AWS CLI, use the curl utility command.

Example command:

curl -IL https://swapnil.live.

Example output:

curl -IL https://swapnil.live  --> Initial Link
HTTP/2 301
server: awselb/2.0
date: Sat, 15 May 2021 09:00:14 GMT
content-type: text/html
content-length: 134
location: https://www.example.com:443/  --> New redirected link 

HTTP/2 200  --> 200 OK Response from backend
content-encoding: gzip
accept-ranges: bytes
age: 155230
cache-control: max-age=604800
content-type: text/html; charset=UTF-8
date: Sat, 15 May 2021 09:00:14 GMT
etag: "3147526947"
expires: Sat, 22 May 2021 09:00:14 GMT
last-modified: Thu, 17 Oct 2019 07:18:26 GMT
server: ECS (dcb/7EEF)
x-cache: HIT
content-length: 648

2.    In your internet browser, enter the domain that you're redirecting to and confirm that it redirects to the target domain.

3.    (Optional) For more detail, review the HTTP Archive (HAR) file. A HAR file is a JSON file that contains the latest network activity recorded by your browser.

Capture the HAR file

Google Chrome:

  1. In the browser, choose Customize and control Google Chrome.
  2. Choose More tools, Developer tools.
  3. With DevTools open in the browser, choose the Network panel.
  4. Select the Preserve log check box.
  5. Choose Clear to clear all current network requests.
    Note: When you reload the page, you can see the redirection happening in the first few requests, depending on the level of redirection configured.
  6. In DevTools, open the context (right-click) menu on any network request.
  7. Choose Save all as HAR with content, and then save the file.

Microsoft Edge (Chromium):

  1. In the browser, choose Settings and more.
  2. Choose More tools, Developer tools.
  3. With DevTools open in the browser, choose the Network panel.
  4. Select the Preserve log check box.
  5. Choose Clear to clear all current network requests.
    Note: When you reload the page, you can see the redirection happening in the first few requests, depending on the level of redirection configured.
  6. In DevTools, open the context (right-click) menu on any network request.
  7. Choose Save all as HAR with content, and then save the file.

Mozilla Firefox:

  1. In the browser, choose Open menu.
  2. Choose Web Developer.
  3. In the Web Developer menu, choose Network.
    Note: In some versions of Firefox, the Web Developer menu is in the Tools menu.
  4. On the Network Monitor, select the Persist Logs check box.
  5. Choose the trash can icon (Clear) to clear all current network requests.
    Note: When you reload the page, you can see the redirection happening in the first few requests, depending on the level of redirection configured.
  6. In the Network Monitor, open the context menu (right-click) on any network request in the request list.
  7. Choose Save All As HAR, and then save the file.

Analyze the HAR file

  1. Open your preferred HAR analyzer, such as Google's HAR Analyzer.
  2. Upload the HAR file that you just created.
  3. Clear the check boxes for all HTTP codes except 3XX.
  4. Choose any of the listed entries for details on the redirection request.

Did this article help?


Do you need billing or technical support?