I have a domain formulagrid.com .
I use AWS S3 to host it as a static website. My problem was that I wanted to redirect the www subdomain to the same domain:
https://www.formulagrid.com -> https://formulagrid.comhttp://www.formulagrid.com -> https://formulagrid.com
Amazon provides redirecting URLs from the S3 bucket to the S3 bucket if they are configured for static website hosting.
So, I needed to create two buckets:
formulagrid.com - actual websitewww.formulagrid.com - exists only to redirect to the actual website.
This works fine if you only work through HTTP, but S3 absolutely does not support HTTPS.
A way to use HTTPS to connect to a static S3 site is to configure CloudFront distribution in front of the S3 bucket. CloudFront, however, although it provides HTTPS, basically exists to work as a CDN.
At first, I had the only CloudFront distribution setup in front of the S3 bucket that hosted the actual site. Everything seemed operational: the site was distributed via CDN, HTTPS and HTTP redirected to HTTPS.
There was one exception.
https://www.formulagrid.com was a completely broken page
After trying to find the source of the error for a while, I realized this because it did not go through the CDN, and trying to access S3 via HTTPS does not work.
Finally, I had to make another distribution, located in front of the www S3 bucket, so that it was accessible via HTTPS. This is where my problems arise, because, as I mentioned earlier, CloudFront's main goal is to be a CDN.
It doesn't seem to me that the CDN is sitting in front of an address that is simply being redirected to another. The question also arises as to whether I will be charged twice for each request that falls into the www subdomain, because after redirecting it got to another CloudFront distribution.
This is disappointing because I'm trying to make a serverless architecture using Lambda, and I only need to provide an EC2 instance to rewrite the URLs, this is not what I want to do if it is not my last resort.
The solution would be trivial if Amazon offered any form of URL rewriting or if CloudFront redirected itself, but none of them exist as far as I know (let me know if they do).
I'm new to AWS, so I hope someone with more experience can point me in the right direction.