How do I redirect an apex domain to its subdomain or any other domain using S3 and Route 53?

3 minute read
0

I want to redirect internet traffic from my root (apex) domain (for instance, example.com) to its subdomain (for instance, www.example.com) using Amazon Simple Storage Service (Amazon S3). Or, I want to redirect internet traffic from my apex domain to another domain (for instance, example.net) using Amazon S3.

Resolution

Prerequisites

  • You have a hosted zone for your apex domain in Amazon Route 53.
  • You have permissions to create records in the hosted zone for the apex domain.
  • You have permissions to create S3 buckets.
  • An S3 bucket with the exact same name as your apex domain doesn't already exist.
    Note: Amazon S3 website endpoints don't support HTTPS. So, redirection works for HTTP requests only. To redirect both HTTP and HTTPS requests, use other methods, such as redirecting requests using an Application Load Balancer or using Amazon CloudFront.

Use the following procedure to redirect your domain using Amazon S3. For example, to redirect requests for the apex domain example.com to its subdomain www.example.com, use following steps:

  1. In the Amazon S3 console, create an S3 bucket with the exact name of your apex domain. For instance example.com.
    Note: S3 bucket names are globally unique. If the bucket name that you need is already in use, then you can't use Amazon S3 for redirection. In this case, consider other methods, such as configuring redirection using an Application Load Balancer or using Amazon CloudFront with an edge function.
  2. Select the bucket that you created, and then choose Properties.
  3. Under Static website hosting, choose Edit.
  4. Choose Redirect requests for an object.
  5. For Host name, enter the website that you want to redirect to. For instance, www.example.com.
  6. For Protocol, choose the protocol for the redirected requests (none, HTTP, or HTTPS).
    Note: If you don't specify a protocol, then the default option is none.
  7. Choose Save changes.
  8. In the Route 53 console, select the hosted zone for your apex domain. For instance, example.com.
  9. Create an A-Alias record for the apex domain in the selected hosted zone with the following values:
    Record name: Leave this field blank.
    Record Type: Choose A – IPv4 address.
    Route traffic to: Choose Alias to S3 website endpoint.
    Region: Choose the Region where your S3 bucket is located.
    Enter S3 Endpoint: From the dropdown list, choose the S3 bucket that you created. For instance example.com. Make sure that the S3 bucket name exactly matches the name of the hosted zone for your apex domain.
    Routing policy: Choose Simple.
    Evaluate Health Target: Choose No, and then choose Create Records.

To validate the redirection, open your apex domain in a browser. Or, use the following curl command to check the HTTP status code for the response and the value of the Location header in the response. A successful redirection returns the HTTP 301 Moved Permanently status code and the Location header value has a URL for the domain that you're redirecting to.

curl -i -s example.com | grep -E "HTTP|Location"
      HTTP/1.1 301 Moved Permanently
      Location: http://www.example.com/

Related information

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

Redirect requests for your bucket's website endpoint to another bucket or domain

AWS OFFICIAL
AWS OFFICIALUpdated a year ago