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

39,840

Solution 1

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

Solution 2

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!

Solution 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.

Solution 4

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/

Share:
39,840
Barrett Kuethen
Author by

Barrett Kuethen

Updated on July 09, 2022

Comments

  • Barrett Kuethen
    Barrett Kuethen almost 2 years

    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

  • Flimm
    Flimm about 8 years
    To get this to work with SSL as well (example.com redirecting to example.com), see this blog post: building.vts.com/blog/2015/11/02/…
  • beterthanlife
    beterthanlife about 8 years
    The downside with this is that you lose your header information (referrer, etc).
  • Nick
    Nick about 7 years
    Note: This method only works for HTTP - HTTPS does not work
  • lapinkoira
    lapinkoira about 6 years
    For HTTPS you need to use cloudfront
  • Eugene
    Eugene over 5 years
    With cloudfront, you can use lambda edge function to ALWAYS redirect to the correct domain
  • BenjaminGolder
    BenjaminGolder over 4 years
    Is there another way to do this that preserves the HTTP headers?
  • stevec
    stevec over 3 years
    @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
  • Kyle Calica-St
    Kyle Calica-St over 3 years
    I wrote an in-depth article with how to do this currently even with cloudfront and a private S3 bucket. medium.com/@kylecalicast/…
  • Aidin
    Aidin about 3 years
    [+/-] 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
    Aidin about 3 years
    To have a holistic coverage of all the required redirects for HTTP and HTTP, naked and www (all 4 cases), see stackoverflow.com/a/67222388
  • Aidin
    Aidin about 3 years
    To have a holistic coverage of all the required redirects for HTTP and HTTP, naked and www (all 4 cases), see stackoverflow.com/a/67222388
  • Christoph Hansen
    Christoph Hansen over 2 years
    "The one exception is when you want to redirect queries from the zone apex (such as example.com) to a record in the same hosted zone that has a type of CNAME (such as zenith.example.com). " docs.aws.amazon.com/Route53/latest/DeveloperGuide/…
  • Josh Hibschman
    Josh Hibschman over 2 years
    Yep, Route53 -> CloudFront (with naked ssl cert) -> Either S3 Static Site Redirect or Lambda Redirect is still the only way to properly do this... hours of work