DNS forwarding

You are hosting an application with Heroku and want it to be reachable through the root domain and the www subdomain (i.e. example.com and www.example.com).

This can be done by adding the domains in Heroku and setting up a CNAME record for example.com pointing to example.com.herokudns.com and a CNAME record for www.example.com pointing to www.example.com.herokudns.com in your DNS.

However, if you also want to use mail on your domain then you need an MX record on example.com pointing to your mail service. This will not work as the CNAME overrules all records on example.com (including the MX record). To fix this you want to set forwarding from example.com to the www.example.com. This can be done with AWS Route 53 by following the steps as desribed in this article from Heroku:

  1. Create S3 bucket with name example.com with static website hosting enabled. Set the the target bucket to www.example.com.
  2. In route 53 create an A record, select yes for ALIAS and fill in your S3 endpoint.

This works, but only for http://example.com and not for https://example.com. This can be resolved by using AWS Cloudfront and AWS ACM (adapted from the steps taken in this article):

  1. Create SSL certificate in Amazon ACM for example.com and www.example.com. Make sure this certificate is valid and in region us-east-1.
  2. Create cloudfront distribution pointing to the S3 bucket. Set Alternate Domain Names (CNAMEs) to example.com and www.example.com and set the SSL Certificate to the one you just generated.
  3. In Route 53, intead of creating an ALIAS record with the S3 bucket as endpoint (as we did in the previous steps), point the record to your cloudfront url.

Now your application can be reached from http:\\example.com, https:\\example.com, http:\\www.example.com, https:\\www.example.com and you can send and receive emails with example.com as extension.