Skip to content

Instantly share code, notes, and snippets.

@eprothro
Last active January 24, 2020 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eprothro/e600d4c3d430a20650136a2053e6bfcb to your computer and use it in GitHub Desktop.
Save eprothro/e600d4c3d430a20650136a2053e6bfcb to your computer and use it in GitHub Desktop.

Heroku DNS

For Heroku to receive traffic at the apex domain (your-tld.com) Your DNS provider must support ANAME / ALIAS resolution for a hostname (your-app-random-heroku-endpoint.herokudns.com) to the apex domain.

GoDaddy does not support this, you can only set up an A record to a static IP address for the apex.

They provide domain forwarding, but this does not work for HTTPS, so requests to https://your-tld.com will timeout. Their domain forwarding service will only work for forwarding from http://your-tld.com.

If you're using GoDaddy, it is recommended to transfer or switch name servers to a different service (e.g. Namecheap, DNSimple, etc).

Rails apex redirection

The simplest way to support the combination of http/https/apex redirection to https://www.your-tld.com is with route matching.

# routes.rb
YourApp::Application.routes.draw

  constraints subdomain: "" do
    match "(*any)" => redirect { |params, request| URI.parse(request.url).tap { |uri| uri.host = uri.host.prepend("www.") }.to_s }
  end
  
# ...

Handling this with rack middleware is more performant, but tricky to get right with SSL. Unless you are expecting massive redirection traffic, this is not recommended.

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