Barrett Kuethen
Barrett Kuethen

Reputation: 1914

How do I redirect a naked (apex) domain to www using Route 53?

I need to do a 301 redirect from example.com to www.example.com using Route 53 (and S3 if necessary). There are a few solutions for similar problems but they either do not address how to redirect from the apex or they simply don't work.

When I follow the steps here, Route 53 tells me I can't add a CNAME to the apex domain. Therefore, I'm stuck in the mud.

This seems dumb simple but Amazon is making it hard. Any help would be appreciated.

enter image description here

Upvotes: 65

Views: 61432

Answers (6)

thisismydesign
thisismydesign

Reputation: 25092

Expanding on @omikes's response with an explanation and SSL support.

Ideally you want all of the following URLs to resolve to the same place:

For technical reasons, DNS CNAME records (sometimes called ALIAS or ANAME) should be preferred over A records to point to the root (sometimes called apex/bare/naked) domains. While some providers support this, AWS Route 53 does not.

Therefore an ideal AWS setup routes all the above to https://www.example.com.

This can be done at no extra cost but requires some extra pieces of infrastructure (~15 min setup). Unless noted use default config.

1, S3 Bucket to redirect to www. subdomain

  • Create a S3 Static Hosting Website and have it redirect to www.example.com.
  • Use https protocol.

2, SSL certificate in Certificate Manager

3, CloudFront distribution

  • Use 'Redirect HTTP to HTTPS'
  • As origin use the full domain of the S3 bucket. Do not select the bucket from the lsit but use the actual domain. E.g. example.com.s3-website-eu-west-1.amazonaws.com
  • For Alternate Domain Names provide example.com
  • Provide SSL certificates created previously

4, DNS record in Route53

  • A Alias record of example.com pointing to CloudFront
  • CNAME record of www.example.com pointing to the service

Upvotes: 7

Diego Bianchi
Diego Bianchi

Reputation: 742

For those who need to redirect a naked domain example.com to www.example.com

The best practice is to create a new Load Balancer

with Listener on port 80 http that redirect 301 to www.example.com

In case you have https running just add another listener on port 443 that redirect 301 to www.example.com

enter image description here

Once you have finished with the listeners go to

Route 53 > your domain> Create Record

that points your new load balancer with the redirect listeners

record name: example.com

Record Type: A

Alias: Yes

Alias to Application and Load Balancer...

Select your location

Select your new Load Balancer

enter image description here

Save it and wait some minutes and now all request for naked example.com redirect 301 to www.example included for https request.

Note: If you want to redirect http request to https request you need to do the same procedure but instead of a redirect listener you need to set-up a forward listener to https.

Upvotes: -3

MatthewT
MatthewT

Reputation: 648

If you are using an Application Load Balancer, they added support for redirects. You point your zone apex at your ELB (using an Alias) and have ELB do the redirect to www.example.com

This seems like a much better solution than setting up a bucket, adding static hosting, redirect, etc.

https://aws.amazon.com/about-aws/whats-new/2018/07/elastic-load-balancing-announces-support-for-redirects-and-fixed-responses-for-application-load-balancer/

Upvotes: 4

omikes
omikes

Reputation: 8513

I just managed to figure this out yesterday:

  1. Go to your S3 console at https://console.aws.amazon.com/s3/home and click Create bucket. For Bucket name enter in your naked domain name, e.g. for www.example.com, you would put just example.com. Then click Create.

  2. Click on the name of the new bucket, then click Properties > Static website hosting. On the menu that appears select Redirect requests. For Target bucket or domain enter in the full domain, such as www.example.com. For Protocol put in the desired protocol, http or https. Click Save.

  3. Open your Route 53 hosted zone by navigating to https://console.aws.amazon.com/route53/home and clicking Hosted zones in the menu to the left. Select your domain and click on Create Record Set. Leave the Name area blank, and leave the default type at A - IPv4 address. Select Yes for Alias, and click on the Alias Target textbox. You may have to wait a few moments for the values in the dropdown menu that appears to populate. At this point the menu should contain the S3 Website Endpoint you created in steps 1 and 2. Select it.

Lastly, click Create, and enjoy your new routing set-up!

Upvotes: 101

ccov77
ccov77

Reputation: 791

The necessary steps are as follows:

  • Create a new Bucket and call it exactly as your naked domain name (eg: example.com).
  • In the Bucket properties, select "Static Website Hosting" and make it redirect to your www.example.com domain. Click "save".
  • Go to your Route53 hosted zone, and create an A record. Mark it as an Alias, and from the dropdown select your recently created bucket.
  • You're ready.

Upvotes: 3

Arya
Arya

Reputation: 2153

Try making an alias record instead of CNAME.

Per this answer: RRSet of type CNAME with DNS name foo.com. is not permitted at apex in zone bar.com

Upvotes: 28

Related Questions