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/.example.com redirects to http://ec2-xxxxxx.amazonaws.com/wp-admin/setup-config.php`These are the things I have tried.
ServerName example.com in /etc/apache2/apache2.confsudo ufw allow in "Apache Full".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:
.htaccess at the root directory as I feel that would create an endless loop.I can provide more information, if required.
Update:
ServerName example.com to /etc/apache2/sites-enabled/000-default.confrandom-id.cloudfront.net does the same 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