Search code examples
http-redirectsslamazon-s3amazon-cloudfrontamazon-route53

Cloudfront redirect www to naked domain with ssl


Forgive me if this has been asked before, there are a number of resources that touch on this, but nothing seems to fit for my specific (https) use-case.

I'm trying to redirect https://www.example.com to https://example.com. Likewise, this should work for http://www.example.com to https://example.com.

I have set up a cloudfront distribution with the origin of an s3 bucket, redirect http to https, added the cname example.com and added my domain certificate (which works for the www subdomain as well as the naked domain).

I have also set up a separate distribution, with a cname for www.example.com, added the certificate and set the origin to a separate s3 bucket that in (static website hosting) redirects all requests to https://example.com.

Redirection works as expected for http://example.com to https://example.com, however http(s)://www.example.com to https://example.com does not.

In route 53 I have the root domain aliased to the first cloudfront distribution and www aliased to the second.


Solution

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