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 toec.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.
2
Answers
Traced the issue to
HTTP_HOST
being the ec2 public DNS received by the server. So, if I visitedexample.com/index.php
and the code were supposed to redirect toexample.com/index2.php
it would instead redirect toec.xxxxxx.amazonaws.com/index2.php
becauseHTTP_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 beexample.com
And if I visit
id.cloudfront.net
then host value would beid.cloudfront.net
.https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23
If I correctly understand your setup, probably you need CNAME instead of A record in Route53