49

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

Community
  • 1
  • 1
Barrett Kuethen
  • 1,624
  • 7
  • 24
  • 32

4 Answers4

76

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!

omikes
  • 6,140
  • 8
  • 33
  • 44
  • 9
    To get this to work with SSL as well (https://example.com redirecting to https://www.example.com), see this blog post: http://building.vts.com/blog/2015/11/02/route53-ssl-naked-domain-redirect/ – Flimm Feb 23 '16 at 10:33
  • 10
    The downside with this is that you lose your header information (referrer, etc). – beterthanlife Apr 21 '16 at 09:07
  • 1
    Is there another way to do this that preserves the HTTP headers? – BenjaminGolder Jan 16 '20 at 04:00
  • @Flimm I didn't try it but the same article is on medium and someone commented that cloudfront doesn't allow that any more. I only note that here because it may save you/someone else some time. **but** I haven't tried so I am not sure – stevec Aug 29 '20 at 05:14
  • I wrote an in-depth article with how to do this currently even with cloudfront and a private S3 bucket. https://medium.com/@kylecalicast/production-level-static-sites-in-aws-39573c9bf4d5 – Kyle Calica-St Sep 13 '20 at 20:03
  • To have a holistic coverage of all the required redirects for HTTP and HTTP, naked and www (all 4 cases), see https://stackoverflow.com/a/67222388 – Aidin Apr 23 '21 at 00:18
24

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

Community
  • 1
  • 1
Arya
  • 2,037
  • 13
  • 12
3

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.
Juan
  • 604
  • 6
  • 12
0

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/

MatthewT
  • 568
  • 5
  • 16
  • [+/-] **Pro:** You can preserve the query. **Cons:** 1) S3 Buckets are free/very cheap, but ELB is at least $15/mo. 2) Also, piggybacking on existing ELB might not be a great idea in terms of handling all sorts of requests in one place. – Aidin Apr 22 '21 at 23:54