skip to Main Content

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:

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:

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


  1. Chosen as BEST ANSWER

    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.

    enter image description here

    https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23


  2. If I correctly understand your setup, probably you need CNAME instead of A record in Route53

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search