Cloudfront redirect www to naked domain with ssl [closed]
Solution 1:
To host website on AWS so that:
https://www.example.com, http://www.example.com and http://example.com all redirect to https://example.com
you need to:
Create two S3 buckets named: example.com and www.example.com.
Turn on the Static Website Hosting on these two buckets.
Configure redirect in bucket www.example.com to: https://example.com. In the bucket properties choose Static Website Hosting => Redirect all requests to another host name. In Target bucket or domain field, enter example.com, in Protocol field, enter https
For these buckets create two CloudFront Distributions. Each of this distributions point to corresponding bucket:
-
For Origin Domain Name provide bucket urls provided in Static Website Hosting section. The urls should have form (or similar): example.com.s3-website-us-west-1.amazonaws.com
On both distribution set HTTP to HTTPS redirect.
DO NOT USE URL SUGGESTED BY AMAZON AUTOCOMPLETE!
DO NOT SET Default Root Object PROPERTY!
Configure DNS by setting A records for www.example.com and example.com to point to corresponding CloudFront distributions.
Why does it work? CloudFront provides the redirect from HTTP to HTTPS in both cases (with and without www). The bucket for www.example.com provides redirect to example.com. If you didn't have this distribution, the bucket would not be able to redirect request for https://www.example.com. S3 itself does not support HTTPS for static website hosting.
Solution 2:
I found the solution thanks to this answer: Amazon S3 Redirect and Cloudfront
In short:
Cloudfront does not respect the redirection rules setup in S3 if the origin is just the bucket ID. Instead I had to set the origin to the provided s3 static website host name.