How to force CloudFront CName to handle non-www requests? - amazon-s3

I'm new to CloudFront and was wondering how to force it to redirect. In Apache it's just through Virtual Host, but with CloudFront, I'm confused how to do it.
Here is my scenario
NameCheap
CNAME | www | random12345.cloudfront.net (where my site files are)
AWS
ACM - requirement for CloudFront for custom SSL (N.Virginia)
Domain Name: sample.com
Additional Domain Name(s): *.sample.com
CloudFront Distribution
Domain Name: random12345.cloudfront.net
CName: sample.com
*.sample.com
Result
WORKED: https://www.sample.com | www.sample.com
NOT WORKING: sample.com | https://sample.com | http://sample.com
Typing these in the search bar results to Error: This site can’t be reached sample.com’s server IP address could not be found. DNS_PROBE_FINISHED_NXDOMAIN
I attempted to create an S3 bucket to force redirect to https://www.sample.com
but can't create since CNAME www.sample.com is already used for the CloudFront Distribution. Can't use # in CNAME since it is not recommended on root configuration due to its effect on MX records
Thanks a lot.

You have to following step for redirecting non-www domain to www :
Note: www.example.com bucket should have code and example.com will be empty bucket only configuration mention below.
Create example.com S3 bucket.
Go to S3 bucket properties -> Static website hosting then select Redirect request add value as www.example.com
Set Bucket policy and CORS same www.example.com bucket
Create CloudFront for example.com use origin as example.com.s3-website-us-east-1.amazonaws.com not which selection dropdown giving.
Select user *.example.com ACM for https
Then create Route53 Entry for this example.com as A record select respective CloudFront origin for example.com.
This domain is transferred to AWS if you don't want full NS record then only move A record to AWS.
Then you are done.

Related

How to setup DNS for CloudFront distribution with ACM for test and production servers pointing to different directory roots?

I am facing difficulty in serving requests from test.domain.com with the configuration with a combination of AWS Certificate Manager (ACM), AWS EC2 (linux AMI), AWS CloudFront (CF), Google DNS and Apache server.
I have following setup in public_html directory:
I have 2 folders in /var/www/html
prod
test
Objective is to serve requests received from test.domain.com using test directory and the ones received from domain.com using prod directory.
The setup is working fine with acme ssl certificates ie., for production (domain.com), we are using acme SSL certificate and DNS is pointing to Elastic IP and working fine. Even test.domain.com was working
fine with acme ssl setup.
However, I'm trying to switch to ACM. As it works only with CF and ELB (AWS Elastic Load Balancer), created a CF distribution.
Created one CloudFront (CF) distribution pointing to AWS EC2 endpoint with Origin path /test.
Redirected test.domain.com to CF distribution in Google DNS as the domain is registered with them.
With this setup, test.domain.com is also presenting domain.com and not the test server as anticipated.
https.conf has correct DocumentRoot for each ServerName. But, request is not hitting the virtualhost of test server..
whats missing? pls suggest..
You can try below:
Add both domain.com and test.domain.com to CloudFront CNAME list.
ACM certificate which has common name/SAN as domain.com and *.domain.com (or test.domain.com)
In CloudFront cache behavior , whiteist HOST header, this will make sure that when client access domain.com , cloudfront send the same value in host header when contacting origin.
Link:
Forward host header

redirect with mask on AWS S3, cloudfront and route53

I have an example.com domain name and I would like to redirect to subdomain.otherexample.com but I don't like that when the redirection occurs the old URL is showing subdomain.otherexample.com how could I do that?
I am actually using S3 and Cloudfront.
Actually, from cloudfront redirect to S3 and a record type A use an alias to redirect to cloudfront
I am using S3 to redirect to the web that I want to show, in static web hosting, my service provider gives me the following sub.example.com url and additional shows two NS,
ns1.example.com and ns2.example.com.
S3> in the redirection I place sub.example.com and the http protocol since it does not use https.
Route53> add two A records with Alias
1. www.mydomain.com to s3-website-us-west-2.amazonaws.com
2. mydomain.com to www.mydomain.com
www.mydomain.com redirects but does not hide sub.example.com and mydomain.com shows 404 error
Code: NoSuchBucket
Message: The specified bucket does not exist
BucketName: mydomain.com
RequestId: DA0B8E051C8F5842
HostId: RBiqPpf6 / l + i0GlT4ZSEPizxT + 3oEr4d1ZoxtyxE3Yhyk / hXEyYDU3 /
ItZe9NsUKkTtmc6wL + g8 =
Thanks for answering

AWS S3 + Route53: Redirecto S3 bucket not doing anything

I have a domain: mydomain.com.
I've created an A record for www.mydomain.com with an alias targeting the load balancer's DNS and works just fine:
When I hit www.mydomain.com everything works fine.
Now, I want to redirect all requests to mydomain.com to www.mydomain.com (Note the www prefix).
So I created an S3 bucket named mydomain.com with a redirection to www.mydomain.com, and an A record for mydomain.com with an alias targeting the S3 bucket named mydomain.com I just created.
The idea here is to make all requests to mydomain.com to go to my S3 bucket and then get redirected to www.mydomain.com.
But when I go to mydomain.com, the browser just keeps loading forever. No redirection seems to be happening.
All I want is to redirect requests to mydomain.com to www.mydomain.com, how can I make this work? Why is my browser loading forever and not giving any error?

Point non www domain to existing cloudfront distribution

I'm using AWS S3 and Cloudfront to host a website (e.g. www.company.com). I want to additionally have the the naked domain (without the www) to point to the same content. I initially created a redirect in DNS but https://company.com didn't work.
I can create an apex record for the naked domain in the DNS but can I point it to the same cloudfront CNAME used for the www.company.com or do I have to create a new S3 bucket and new cloudfront distribution?
S3 to CloudFront Distribution
Create two CloudFront distributions
Request for Certificates from AWS Certificates Manager
Create CNAME with Route 53 and point the alias target to the respective distributions
Create Origin in both distributions pointing to that S3 bucket
Hope it helps.
Another solution using only one Cloudfront distribution, if redirecting company.com to www.company.com is acceptable (usually it's preferred).
Create an S3 bucket named company.com
Configure the bucket for static website hosting. Choose "Redirect requests for an object" and enter www.company.com.
Update your DNS a record to point to the bucket.

AWS Route 53, naked domain automatically routes to www

I have hosted a static website on S3, with Cloudfront distribution using the "Hosting a static website" quick start option on AWS.
I added an A record (alias) to the cloudfront distribution for the apex, and added another A record (alias) to the same distribution for the www subdomain.
soyou.co redirects to www.soyou.co and fails with an error. The cloudfront distribution url works fine. I purchased the domain in Godaddy, and added the Amazon Name Server (NS) records in Godaddy.
Where could I be wrong? Thanks in advance.
For the www subdomain you need to add a CNAME record pointing to your domain name. Delete the A record alias for the subdomain.