Reputation: 1914
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.
Upvotes: 65
Views: 61432
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
https
protocol.2, SSL certificate in Certificate Manager
3, CloudFront distribution
example.com.s3-website-eu-west-1.amazonaws.com
4, DNS record in Route53
Upvotes: 7
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
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
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
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.
Upvotes: 4
Reputation: 8513
I just managed to figure this out yesterday:
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.
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.
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
Reputation: 791
The necessary steps are as follows:
Upvotes: 3
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