Skip to content

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 use Amazon Simple Storage Service (Amazon S3) and Amazon Route 53 to redirect traffic from my apex domain to its subdomain or to another domain.

Resolution

Prerequisites:

  • Confirm that you have a hosted zone for your apex domain in Route 53.
  • Verify that you have permissions to create records in the hosted zone for the apex domain and to create Amazon S3 buckets.
  • Make sure that an Amazon S3 bucket with the same name as your apex domain doesn't already exist.

Create an S3 bucket to redirect your domain

Note: Amazon S3 website endpoints don't support HTTPS. To redirect both HTTP and HTTPS requests, use other methods such as an Application Load Balancer or Amazon CloudFront with an edge function. For more information, see How do I redirect a domain in CloudFront? and How can I use an Application Load Balancer to redirect one domain to another?

Complete the following steps:

  1. In the Amazon S3 console, create an Amazon S3 bucket with the exact name of your apex domain.
  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.
  6. For Protocol, choose HTTP or HTTPS for the redirected requests.
    Note: If you don't specify a protocol, then the default option is none.
  7. Choose Save changes.

Create an Alias record to point to the S3 bucket

Complete the following steps:

  1. In the Route 53 console, select the hosted zone for your apex domain.
  2. Create an A-Alias record for the apex domain in the selected hosted zone with the following values:
    For Record name, leave this field blank.
    For Record Type, choose A – IPv4 address.
    For Route traffic to, choose Alias to Amazon S3 website endpoint.
    For Region, choose the AWS Region where your Amazon S3 bucket is located.
    For Enter Amazon S3 Endpoint, from the dropdown list, select the Amazon S3 bucket that you created. Make sure that the Amazon S3 bucket name exactly matches the name of the hosted zone for your apex domain.
    For Routing policy, choose Simple.
    For Evaluate Health Target, choose No.
  3. Choose Create Records.

Confirm the redirection

To confirm the redirection, open your apex domain in a browser. Or, run the following curl command to check the response headers:

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

Example output:

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

Note: A successful redirection returns the HTTP 301 status code.

Related information

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

1 Comment

Hi Franklin,

I followed the steps you provided to map the secondary domain to point to the primary domain that is serving content from s3. It is not redirecting. I want to redirect example2.com to example1.com, users should still see example2.com in the url and content should be the same as if one is going to example1.com. Can you help?

replied a year ago