AWS S3 + Route53: Redirecto S3 bucket not doing anything - amazon-s3

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?

Related

Apache redirecting example.com to ec2 public DNS

Setup
Route 53 Domain example.com has A record to id.cloudfront.net and AAAA record to random-id.cloudfront.net.
Cloudfront has origin domain as EC2 public DNS ec.xxxxxx.amazonaws.com and alternate domain as example.com. Also, it has a AWS certificate for SSL/TLS (for now it accepts both HTTP & HTTPS requests)
EC2 is an Ubuntu-20.04 AMI with apache2, php7.4 and phpmyadmin, all installed using apt-get.
Issue is whenever I put in example.com/* the site is automatically redirected to ec.xxxxxx.amazonaws.com/*.
Example:
example.com/phpmyadmin gets redirected to ec.xxxxxx.amazonaws.com/phpmyadmin/.
I tried to setup wordpress and example.com redirects to http://ec2-xxxxxx.amazonaws.com/wp-admin/setup-config.php`
These are the things I have tried.
Added ServerName example.com in /etc/apache2/apache2.conf
This command sudo ufw allow in "Apache Full"
I do not have .htaccess in my /var/www/html folder.
Is it a Cloudfront or an Apache issue? This is my first rodeo with AWS and Apache and trying to learn how it works.
Solutions that seem out of scope:
https://serverfault.com/questions/685654/apache-server-config-redirect-from-ip-to-domain-name-ec2
Or anything that includes creating .htaccess at the root directory as I feel that would create an endless loop.
I can provide more information, if required.
Update:
Removed A and AAAA record from route53. Doesn't work.
Also, added ServerName example.com to /etc/apache2/sites-enabled/000-default.conf
Also, visiting random-id.cloudfront.net does the same redirection.
However, visiting elastic-IP does not do any redirection.
If I correctly understand your setup, probably you need CNAME instead of A record in Route53
Traced the issue to HTTP_HOST being the ec2 public DNS received by the server. So, if I visited example.com/index.php and the code were supposed to redirect to example.com/index2.php it would instead redirect to ec.xxxxxx.amazonaws.com/index2.php because HTTP_HOST = ec.xxxxxx.amazonaws.com.
Fixed by updating CloudFront Distribution's behavior to forward Host value.
So, if I visit example.com the host value would be example.com
And if I visit id.cloudfront.net then host value would be id.cloudfront.net.
https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23

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

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.

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

Heroku naked domain problem with Google Domains

I have a question about Heroku.
When I try to add new domain with example.
the new domain has to be www.example.com.
If I use example.com. it has ACM problem, no matter how I change the setting in google domains, it goes wrong.
Then fine, I keep the www.example.com one, and web works with setting in google domain
www CNAME 1h www.example.com.herokuapp.com
however, for some reason, I need to add a new subdomain:
auth.example.com
and in my googles domain setting,DNSdata is connect others,not heroku
auth CNAME 1H someDNSdata
then the problem comes out, there is automatically SSL for my web, but it only apply to www.example.com. it doesn't apply to auth.example.com.
i can't adda new domain auth.example.com in heroku because there is
auth CNAME 1H someDNSdata
in the google setting.
I want my SSL in heroku apply for all subdomain.so what should i do??
PS:i have forward
example.com -->https://www.example.com
in additional problem,
if i type www.example.com,it goes http one,
if i type example.com, it goes https one.
why would that happen?? I set # in subdomain forwarding, but it comes out
example.com -->https://www.example.com
doesn't # mean all subdomain will go to https://www.example.com??