Skip to content

Instantly share code, notes, and snippets.

@electricg
Last active February 12, 2022 16:40
Show Gist options
  • Save electricg/92d2782284b94cfd672622893c813ad3 to your computer and use it in GitHub Desktop.
Save electricg/92d2782284b94cfd672622893c813ad3 to your computer and use it in GitHub Desktop.
How to setup Gandi.net domain routing to Heroku

How to setup Gandi.net domain routing to Heroku

TODO: setup SSL.


This is a quick guide on how to setup domain and infinite subdomains, in a case where, for example, you may have one subdomain for each client.

Starting from this http://stackoverflow.com/a/39646701

Unfortunately, ALIAS records are not used by Gandi, and A records are not available for use with Heroku because Heroku does not use fixed IP addresses. So we have to use www.mydomain.com as base url instead of mydomain.com and set up a redirect from the root domain to the www. version.

Heroku side

through their CLI (because the internet says their web interface is broken, I don't know if it's accurate), type:

heroku domains:add mydomain.com --app my-app

and

heroku domains:add *.mydomain.com --app my-app

Gandi side

  • Use the Gandi default DNS servers

  • Edit the zone file to be:

     @ 10800 IN A 217.70.184.38
     * 10800 IN CNAME wildcard.mydomain.com.herokudns.com.
     www 10800 IN CNAME mydomain.com.herokudns.com.
    

    Note that the trailing . is required

  • Set web forwarding for the domain:

    The type of forwarding will be 'direct (permanent)', leave the subdomain blank, and set the forwarding address as www.mydomain.com

Your code

Using Node.js and Express, create a middleware that checks for the subdomains

app.use(function(req, res, next) {
  var domains = req.subdomains;
  
  if (/* condition */) {
    // your code
    req.url = '/something' + req.url;
  }
  else {
    // your code
    return res.redirect(somewhere);
  }
  
  next();
  
});
@GarryOne
Copy link

@mercury2768, yes, you need to delete the A record. Heroku doesn't work with A records, Only CNAME/ALIAS because of their dynamic servers infrastructure, they can't give you a static/fixed IP.
You can read more about this here:
https://devcenter.heroku.com/articles/apex-domains
https://devcenter.heroku.com/articles/custom-domains#configuring-dns-for-root-domains

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