How to redirect an apex domain to a subdomain on AWS

blog-image

This post was originally posted on Interesting Retrospectives.

Here is a very detailed scenario: I have set up all all https redirect for my subdomains except the apex domain itself. Say the following are good:

AWS gave me a concrete answer in a tutorial video, AWS KC Videos: How do I redirect (apex) domain to my website hosted on EC2 in AWS? - YouTube. But it is actually redundant, especially there is no need to create an extra S3 bucket since nginx is enabled on our server.

So here is what I did:

  • In Route 53, create a new name record, select A, alias use the current load balancer (as you want all your request are directed to https).
  • In the server’s nginx configuration file, add these lines to the target domain or subdomain you want to deal with. In our case, it’s the apex domain example.com.
location / {
        proxy_pass http://localhost:port;
}
location ~ ^/$ {
        return 301 https://example.com/;
}

The you restart nginx with sudo systemctl restart nginx.

comments powered by Disqus