Skip to content

Instantly share code, notes, and snippets.

@genemyers
Last active March 21, 2018 16:58
Show Gist options
  • Save genemyers/c2fb76bfacf47b715834020216d89996 to your computer and use it in GitHub Desktop.
Save genemyers/c2fb76bfacf47b715834020216d89996 to your computer and use it in GitHub Desktop.
Setting up Raw Domains on Heroku with DNS Made Easy

The expected behavior

Our default expected behavior to almost all apps is that the naked domain (like mydomain.com) get redirected to our real app at a subdomain (common to be www.mydomain.com).

We use this as our default and main solution, since Heroku says that it does not support naked domains (like mydomain.com) using DNS A-records. Heroku have a document explaining it's decision and limitations to this model.

How to configure Heroku app?

You should configure canonical host and Heroku custom domains doing:

$ heroku config:add CANONICAL_HOST=www.mydomain.com
$ heroku domains:add mydomain.com
$ heroku domains:add www.mydomain.com

This way rack-canonical-host middleware will redirect all requests to your app to the correct domain (www.mydomain.com) if the request comes from any other domain, like mydomain.com or mydomain.herokuapp.com. This is necessary because your app should only be acessible from one domain. This prevents SEO concerns, security issues and is more consistent.

How to configure the DNS server?

Read first

Godaddy and registro.br doen't support alias or aname configuration for domains with Heroku. If you insist to use these services the app will not respond to domains without www.

Mode 1 (easiest)

If you use Zerigo DNS Add-on you need configure nothing more. The redirects from naked domain to the canonical host will just happens.

Mode 2

Modern DNS servers like DNS simple and DNS made easy support CNAME-like functionality at naked domain level. It's called ALIAS at DNSimple and ANAME at DNS Made Easy.

On this kind of DNS servers you should configure like if you was configuring a regular CNAME.

ALIAS   mydomain.com       mydomain.herokuapp.com.
CNAME   www.mydomain.com   mydomain.herokuapp.com.

Conclusion

With this completed, we have the following results:

myapp.herokuapp.com   redirects to   www.myapp.com
myapp.com             redirects to   www.myapp.com
www.myapp.com         answers the request

So our content is available in just one url while others give the redirect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment