How do I set up DNS for an apex domain (no www) pointing to a Heroku app?

后端 未结 4 388
北恋
北恋 2020-12-04 05:15

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 r

4条回答
  •  暖寄归人
    2020-12-04 05:55

    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:

    • ALIAS at DNSimple
    • ANAME at DNS Made Easy
    • ANAME at easyDNS
    • ALIAS at PointDNS
    • CNAME at CloudFlare

    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.

提交回复
热议问题