How to redirect non-www traffic to www for site hosted on EC2 instance behind CloudFront?

流过昼夜 提交于 2019-12-03 18:56:27

Like I described in this answer to a slightly different question, the solution is to create a second CloudFront distribution in front of an empty bucket for the variant of your domain name that you want to redirect.

Configure the bucket to redirect, and when you configure the second CloudFront distribution to point to that redirecting bucket, be sure you type in the web site hosting endpoint for the bucket -- don't select the bucket from the Origin Domain Name drop-down list.

Remove example.com from the Alternate Domain Name setting on the existing distribution, and configure it on the new one.

You can use the same ACM cert on both distributions.

Point example.com to the new CloudFront distribution in DNS.

The short answer is: There is no good way. (Personally, I don't consider hacky workarounds a solution.)

The long answer is:

There is no good way, but there is a way. I guess.

I've seen S3 and EC2 recommended by AWS staff in their forums. S3 is a non-option as you've noted. EC2 is probably the most "correct" until route53 or cloudfront can handle this.

  1. Create an ec2 web server and install ssl suitable for example.com (letsencrypt)
  2. Configure it to redirect example.com -> www.example.com
  3. Add an A record that points to the ec2 server
  4. (This won't work because EC2 doesn't support public IPv6) Add an AAAA record that points to the ec2 server

You can probably get away with skipping IPv6 support, but if you need it, ELBs support it -- so have fun with that.

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