99

I already added a custom domain to my Heroku app and it works with www.domain.com.

I need to know how to set up the domain without www to resolve to the app, too.

Here are my current DNS settings:

$TTL 86400
@   IN SOA ns1.first-ns.de. postmaster.robot.first-ns.de. (
    2013041500   ; serial
    14400        ; refresh
    1800         ; retry
    604800       ; expire
    86400 )      ; minimum

@                        IN NS      robotns3.second-ns.com.
@                        IN NS      robotns2.second-ns.de.
@                        IN NS      ns1.first-ns.de.

@                        IN A       88.198.38.XXX
localhost                IN A       127.0.0.1
mail                     IN A       88.198.38.XXX
ftp                      IN CNAME   www
imap                     IN CNAME   www
loopback                 IN CNAME   localhost
pop                      IN CNAME   www
relay                    IN CNAME   www
smtp                     IN CNAME   www
www                      IN CNAME   appname.herokuapp.com.
@                        IN MX 10   mail

What are the correct settings to use so that both example.com and www.example.com would point correctly to my Heroku app?

Boann
  • 48,794
  • 16
  • 117
  • 146
mrks
  • 5,439
  • 11
  • 52
  • 74

4 Answers4

151

(Note: root, base, apex domains are all the same thing. Using interchangeably for google-foo.)

Traditionally, to point your apex domain you'd use an A record pointing to your server's IP. This solution doesn't scale and isn't viable for a cloud platform like Heroku, where multiple and frequently changing backends are responsible for responding to requests.

For subdomains (like www.example.com) you can use CNAME records pointing to your-app-name.herokuapp.com. From there on, Heroku manages the dynamic A records behind your-app-name.herokuapp.com so that they're always up-to-date. Unfortunately, the DNS specification does not allow CNAME records on the zone apex (the base domain). (For example, MX records would break as the CNAME would be followed to its target first.)

Back to root domains, the simple and generic solution is to not use them at all. As a fallback measure, some DNS providers offer to setup an HTTP redirect for you. In that case, set it up so that example.com is an HTTP redirect to www.example.com.

Some DNS providers have come forward with custom solutions that allow CNAME-like behavior on the zone apex. To my knowledge, we have DNSimple's ALIAS record and DNS Made Easy's ANAME record; both behave similarly.

Using those, you could setup your records as (using zonefile notation, even tho you'll probably do this on their web user interface):

@   IN ALIAS your-app-name.herokuapp.com.
www IN CNAME your-app-name.herokuapp.com.

Remember @ here is a shorthand for the root domain (example.com). Also mind you that the trailing dots are important, both in zonefiles, and some web user interfaces.

See also:

Remarks:

  • Amazon's Route 53 also has an ALIAS record type, but it's somewhat limited, in that it only works to point within AWS. At the moment I would not recommend using this for a Heroku setup.

  • Some people confuse DNS providers with domain name registrars, as there's a bit of overlap with companies offering both. Mind you that to switch your DNS over to one of the aforementioned providers, you only need to update your nameserver records with your current domain registrar. You do not need to transfer your domain registration.

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
kch
  • 77,385
  • 46
  • 136
  • 148
  • 1
    Also, see [Heroku's Dev Center](https://devcenter.heroku.com/articles/apex-domains) – Jon Mountjoy Apr 23 '13 at 15:21
  • 3
    +1 for mentioning dnsimple. Their efforts to make it easier to work with cloud providers was a big factor in my decision to switch to them. – Subfuzion Jun 22 '13 at 05:27
  • 1
    @kch do you know any way I can create a DNS record type that behaves like DNSimple's ALIAS in BIND? – wlf Aug 30 '13 at 08:30
  • 1
    Don't think you can. It's a custom feature. – kch Aug 31 '13 at 20:26
  • cPanel offers a redirect where I configured http://example.com to point to http://www.example.com which then uses the cname record. I am sure some of the linked docs state this, but adding here in case this helps someone else. – pherris Jun 30 '14 at 11:39
  • 2
    CloudFlare also provides this features through what they call [CNAME flattening](https://support.cloudflare.com/hc/en-us/articles/200169056-CNAME-Flattening-RFC-compliant-support-for-CNAME-at-the-root). They automatically do this for CNAME records you create at the root but they will enable it on your whole zone for all CNAME records if you get in touch with them and ask for it. – Adrian Frühwirth Nov 18 '14 at 17:03
  • I've just found nsone.net, which offers ALIAS records, and has a free tier for less than 500k requests per month: https://nsone.net/pricing/ – Robin Winslow Sep 20 '15 at 19:54
  • Clear answer. Thanks. – jofftiquez Aug 07 '16 at 13:29
5

To point your apex/root/naked domain at a Heroku-hosted application, you'll need to use a DNS provider who supports CNAME-like records (often referred to as ALIAS or ANAME records). Currently Heroku recommends:

Whichever of those you choose, your record will look like the following:

Record: ALIAS or ANAME

Name: empty or @

Target: example.com.herokudns.com.

That's all you need.


However, it's not good for SEO to have both the www version and non-www version resolve. One should point to the other as the canonical URL. How you decide to do that depends on if you're using HTTPS or not. And if you're not, you probably should be as Heroku now handles SSL certificates for you automatically and for free for all applications running on paid dynos.

If you're not using HTTPS, you can just set up a 301 Redirect record with most DNS providers pointing name www to http://example.com.

If you are using HTTPS, you'll most likely need to handle the redirection at the application level. If you want to know why, check out these short and long explanations but basically since your DNS provider or other URL forwarding service doesn't have, and shouldn't have, your SSL certificate and private key, they can't respond to HTTPS requests for your domain.

To handle the redirects at the application level, you'll need to:

  • Add both your apex and www host names to the Heroku application (heroku domains:add example.com and heroku domains:add www.example.com)
  • Set up your SSL certificates
  • Point your apex domain record at Heroku using an ALIAS or ANAME record as described above
  • Add a CNAME record with name www pointing to www.example.com.herokudns.com.
  • And then in your application, 301 redirect any www requests to the non-www URL (here's an example of how to do it in Django)
  • Also in your application, you should probably redirect any HTTP requests to HTTPS (for example, in Django set SECURE_SSL_REDIRECT to True)

Check out this post from DNSimple for more.

Jeff Bowen
  • 5,904
  • 1
  • 28
  • 41
3

I am now using Google Apps (for Email) and Heroku as web server. I am using Google Apps 301 Permanent Redirect feature to redirect the naked domain to WWW.your_domain.com

You can find the step-by-step instructions here https://stackoverflow.com/a/20115583/1440255

Community
  • 1
  • 1
Leo Tulipan
  • 300
  • 3
  • 8
2

You are not allowed to have a CNAME record for the domain, as the CNAME is an aliasing feature that covers all data types (regardless of whether the client looks for MX, NS or SOA records). CNAMEs also always refer to a new name, not an ip-address, so there are actually two errors in the single line

@                        IN CNAME   88.198.38.XXX

Changing that CNAME to an A record should make it work, provided the ip-address you use is the correct one for your Heroku app.

The only correct way in DNS to make a simple domain.com name work in the browser, is to point the domain to an IP-adress with an A record.

krisku
  • 3,916
  • 1
  • 18
  • 10
  • Ok but how can I find out my IP? Via host appname.herokuapp.com/www.domain.com I just get an IP like that: 107.20.162.205 (No such Heroku App) – mrks Apr 16 '13 at 09:43
  • 1
    That IP is the one to use. You just cannot test in the browser with an IP-address, because Heroku needs to know the name of the app you are trying to use. Just set up the A record in DNS and test with than, and Heroku will see the name you are using and all should be well! – krisku Apr 16 '13 at 11:18
  • Ok but I get every time an another IP after "host appname.herokuapp.com". – mrks Apr 16 '13 at 12:37
  • 2
    `host app-name.herokuapp.com` results are dynamic. If you hardcode those, your app will eventually brake. See ANAME/CNAME solutions recommended in another comment. – kch Apr 16 '13 at 15:33
  • 1
    You need some webserver with a non-changing IP address that can perform an HTTP redirect from domain.com -> www.domain.com, and point domain.com to that IP address. Then you can have a CNAME for www.domain.com point to the real heroku app. – krisku Apr 18 '13 at 11:40