How to forward non WWW to WWW with AWS Amazon Cloud front behind HTTPS cloud front

眉间皱痕 提交于 2019-12-05 17:51:54

Here's another way of solving the problem. I actually tried to do exactly what you did, and I could never seem to truly redirect the non wwww to the www when CloudFront was involved.

Once you set CloudFront to redirect all HTTP to HTTPS, it handles all of that redirection so you don't need to worry about it. You can take it out of the logic equation.

You should have 4 "A" type records in your Route 53 for the domain (assuming you have IPv6 enabled in CloudFront):

  • www.domain.com "A" alias record pointing to your CloudFront instance
  • www.domain.com "AAAA" alias record pointing to your CloudFront instance
  • domain.com "A" alias record pointing to your CloudFront instance
  • domain.com "AAAA" alias record pointing to your CloudFront instance

And lastly, In the CloudFront "Alternate Domain Names (CNAMEs)" field in the general CloudFront configuration, make sure it contains the www and the non www. Do note that CloudFront changes actually take quite a while to propagate, so be deliberate and patient with your changes.

The end result of this is that both www and non www will work, without any apparent redirection from non www to www, but there is still HTTPS redirection.

I have two CloudFront distributions to achieve this, one serving "example.com" for non-www to www redirection, and the second serving actual content from "www.example.com".

To do this I added a CloudFront Edge Lambda function on the non-www CloudFront distribution that redirects all requests to their www equivalent. I wrote a blog post with the details.

For an alternative approach without using Lambda, this answer points the non-www distribution to an S3 bucket set to redirect all requests to the www version.

You can redirect your naked domain under SSL to the "www" version of it (or any other) without any complicated DNS set-up or changing your provider by using https://www.nakedssl.com – it's free for one domain.

NakedSSL will create and host an SSL certificate for your naked domain (https://yourdomain.com), redirecting it to the HTTPS URL that you want (most likely "https://www.yourdomain.com").

I hope this is helpful for you!

Disclaimer: I'm part of the team that created NakedSSL.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!