S3 Cloudfront - Redirects to the file path - amazon-web-services

I have configured Cloudfront & S3 to redirect my page such that when the user loads www.example.com, it redirects to https://example.com.
When I load the page for url www.example.com it redirects to https://www.example.com.s3-us-west-1.amazonaws.com/index.html. Any idea what am I missing here? I did generate an SSL cert for both example.com and www.example.com. The cloudfront is updated for both www.example.com and example.com.
For www.example.com
The origin domain name path: www.example.com.s3.amazonaws.com
Origin id is S3-www.example.com
For example.com
The origin domain name path: example.com.s3.amazonaws.com
Origin id is S3-example.com
For S3 bucket: www.example.com is redirected to example.com using static website setting - checking Redirect all requests to another host name
Update
I had two distribution settings. One for www.example.com & another for example.com
The https characters are in red with a exclamation mark. (invalid cert) But the certificates are valid
Yes the bucket is recently created and its in the us-west-2 region.
I also created two route 53 Alias records. One for example.com and another for www.example.com
Each points to its own cloud front distribution.

Related

How to configure same origin for apex (example.com) and subdomain (www.example.com) - aws Route53 and CloudFront

I can redirect the www.example.com to example.com using CNAME or A record in route53 but the problem is I want single origin (example.com) in both cases because my api only allow origin example.com, so if I use www.example.com in the browser my api doesnt recognize it and throws cors error.
Is there any way to configure this in such a way that in both cases origin header remains same (if I use www.example.com, the orgin header becomes example.com so that i dont get cors error)
A DNS record is not a redirect. A redirect means when the user visits www.example.com, a 301 status code is returned with the Location header set to the URL of the domain example.com. You can do this by using S3 static website hosting with webpage redirect configured + CloudFront to enable HTTPS.

Route 53 : www traffic redirecting to naked domain

These are the steps I followed:
got a new domain name from AWS domain registry. Lets say example.com
Create a certificate with the help of ACM for example.com
My Cloudfront distribution(lets say dXXXXXXXXXX.cloudfront.net) points to a S3 folder (contains some images)
Cloudfront url with aws provided distribution name works fine (lets say: https://dXXXXXXXXXX.cloudfront.net/1.jpeg)
Added example.com as alternate domain in that distribution definition and imported the certificate of example.com in CloudFront
Added one A and one AAA entry in the route53 where source is: example.com and target is:dXXXXXXXXXX.cloudfront.net
7.Added an alias type 'A' entry where source is www.example.com and target is example.com
8.Now when I try to use url: https://www.example.com/1.jpeg , it is showing can't connect to the server of www.example.com where as if I use https://example.com/1.jpeg , it shows the picture.
Please help me here.
You should use CNAME instead of Alias A.
Try this instead
www.example.com CNAME example.com
example.com A Alias <_id>.cloudFront.com

AWS cloudfront not working with custom domain

I am trying to use cloudfront for static website s3 with my custom domain.
Following are the steps I followed:
1) Setup a s3 bucket (say, example.com) and enabled static website hosting on it.
2) Also setup a s3 bucket (www.example.com) which redirects to example.com.
3) In route 53, added a hosted zone (example.com) and added the record sets.
4) After this, http://example.com works for me.
Now I am trying to add cloudfront to it. I added the following steps:
5) From Amazon Certificate Manager, added a certificate for www.example.com and got it verified (added to Route 53 DNS, it was verified automatically after some time).
6) Created a cloudfront distribution with following settings:
Domain Origin: www.example.com
Origin Protocol Policy: HTTP Only
Alternate Domain Name: www.example.com
SSL Certificate: Selected from ACM
When I try to launch: https://example.com or https://www.example.com, the site doesn't load. http://example.com does load, but I am not sure if cloudfront is actually working on this or not. Also why is https not loading?
To setup the S3 bucket behind the CF distribution WITH SSL you need to:
Setup S3 bucket example.com (Block all public access = off, policy https://d.pr/i/KU1Q4z)
Create certificate in ACM issued at example.com and *.example.com(or specific subdomain at will), validate it
Create CF distribution
Set created CF alternate domain names to: example.com *.example.com (other subdomain here)
Use custom SSL certificate (previously created and validated)
Create/change default origin, to: example.com.s3-website-AWS_REGION.amazonaws.com with origin protocol policy HTTP Only
CF Default origin behaviour should be more-less like this: https://d.pr/i/h6PrG6
In Route 53 set CF A ALIAS for example.com and CNAME for *.example.com (or other subdomain) pointing at CF_DISTRIBUTION_ID.cloudfront.net
you need to go into rt 53 and point the domain at your cloudfront distribution. It won't appear as an option unless you've set the domain as an alternate domain in the distribution settings. Also, that cert won't work for anything except www.example.com, meaning example.com is excluded. you need a cert that includes example.com and www.example.com (or *.example.com to cover all subdomains)

how can I redirect example.com to www.example when I use s3 and cloudfront

I am using AWS Cloudfront to distribute my homepage.
http://www.example.com, https://example.com, http://example.com
All three URLs must be redirected to the main url https: // www.example.com.
The problem is that the https://example.com domain failed to redirect.
I created a www.example.com bucket on s3 and distributed it to cloudfront with an SSL certificate (user aws certificate manager). and I connect cloudfront endpoint to www.example.com url used by route53. So the http://www.example.com => https://www.example.com redirect is a success.
Then I created an example.com bucket on s3 and move to properties > static website hosting > redirect requests then setting target bucket = www.example.com and protocol = https
The result is that http://example.com succeeds in redirecting to https://www.example.com, but https://example.com to https://www.example.com fails to connect.
Did you solve the same problem?
Thank you.
update my cloudfront settings
Here is how I would solve it,
SSL Certificates:
example.com
*.example.com
CloudFront Distribution 1:
CNAME example.com
Certificate: example.com
Viewer Protocol Policy -- Redirect HTTP to HTTPS
Origin and Behaviour --
To S3 Bucket to redirect
Redirect to https://www.example.com
CloudFront Distribution 2:
CNAME www.example.com
Certificate: www.example.com
Viewer Protocol Policy -- Redirect HTTP to HTTPS
Also, note *.example.com certificate won't match example.com certificate.
Based on my experience, I bet I can guess what's happening here. On your Origin configuration in CloudFront, you can set the Origin Protocol Policy to connect to the origin with HTTP, HTTPS or Match Viewer. If you have it set to Match Viewer it will attempt to connect to the S3 Static hosting for your domain via HTTPS, which can be problematic. Instead, set it to HTTP and then things will probably work.
Also, you might want to split your example.com distribution from your www.example.com distribution, as CloudFront differentiates behaviours associated with origins usually based on Path.

Point www subdomain to naked domain instead of bucket name

I've got a domain name (example.com) which I've purchased through namecheap. The website is a just static site that I recently decided to switch over to S3. Following the instructions on the aws website I've:
Created two buckets: example.com and www.example.com
Configured the example.com bucket to be a static website and placed my files in it.
Configured the www.example.com bucket to redirect to the example.com bucket
Created a hosted zone for my domain
Placed a type A alias for example.com in my hosted zone that points to my example.com bucket
Placed a type A alias for www.example.com in my hosted zone that points to my www.example.com bucket
Taken the nameservers from the NS record in my hosted zone and in namecheap set my domain to use them.
When I go to example.com it works just as expected. When I go to www.example.com it takes me to my website correctly but it shows www.example.com.s3-website.us-east-2.amazonaws.com as the domain. I haven't been able to figure out how to get it to point to www.example.com instead. Where have I gone wrong?
Configured the www.example.com bucket to redirect to the example.com
bucket
You want the actual redirect url configured in S3 to be https://example.com, not the url of your example.com bucket. Just update that and you should be good to go.