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.
But 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:
- Create S3 bucket with name
example.com
with static website hosting enabled. Set the the target bucket towww.example.com
. - In route 53 create an A record, select yes for ALIAS and fill in your S3 endpoint.
This works for http://example.com
but not for https://example.com
. This can be resolved by using AWS Cloudfront and AWS ACM (adapted from the steps taken in this article):
- Create SSL certificate in Amazon ACM for
example.com
andwww.example.com
. Make sure this certificate is valid and in regionus-east-1
. - Create cloudfront distribution pointing to the S3 bucket endpoint (Note: do not select the s3 bucket from the dropdown provided but go to the S3 bucket to find the endpoint. It should look like
example.com.s3-website-eu-west-1.amazonaws.com
). SetAlternate Domain Names (CNAMEs)
toexample.com
andwww.example.com
and set theSSL Certificate
to the one you just generated. - In Route 53 point the ALIAS record to your cloudfront url instead of the S3 bucket.
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 from example.com
.