Heroku

How It Works

Custom Domains

Last Updated: 26 January 2012

custom domain dns subdomain

Table of Contents

All Heroku apps are accessible via their heroku.com subdomain: appname.heroku.com. Enabling the Custom Domains add-on allows you to assign additional custom and wildcard domain names.

After assigning a custom domain to your app, you must also point DNS to Heroku. You can use the Zerigo DNS add-on for completely managed DNS service, or use your own DNS provider and manually configure the records for your domains.

Heroku Setup

You must add both www.yourdomain.com and youdomain.com if you want both to work. You must specify each host you want Heroku to recognize.

The first step is to tell Heroku to respond to requests at your domain. This requires adding the custom_domain add-on, and telling Heroku what domain to respond to.

$ heroku addons:add custom_domains
Adding custom_domains to myapp...done.

Then inform Heroku of the domain you are using:

$ heroku domains:add www.example.com
Added www.example.com as a custom domain name to myapp.heroku.com
$ heroku domains:add example.com
Added example.com as a custom domain name to myapp.heroku.com

You can add any number of domains to a single app by repeating the add command with different values.

Remove domains with:

$ heroku domains:remove myapp.mydomain.com
Removed www.example.com as a custom domain name to myapp.heroku.com

You can also clear all the domains at once (often the easiest even if you only have one domain):

$ heroku domains:clear
Removed all domain names for myapp.heroku.com

DNS Setup

Next, you must point your DNS to Heroku. We recommend using the Zerigo Add-On, which will automate all the steps in this section. Alternatively, you may use any DNS provider you wish, following these instructions.

This screencast walks through the steps to setup a custom domain name with GoDaddy:

Root Domains (mydomain.com)

Root domains (mydomain.com) must use A records To setup your root domain, add separate A records for each of the following addresses using your DNS management tool:

75.101.163.44
75.101.145.87
174.129.212.2

Check that your DNS is configured correctly with the “host” command:

$ host example.com
example.com has address 75.101.163.44
example.com has address 75.101.145.87
example.com has address 174.129.212.2

Subdomains ( www.mydomain.com)

For each host you want to setup, configure your DNS provider to make your host name be an alias which points to YOURAPPNAME.heroku[app].com. In DNS terms this is known as a CNAME.

Aspen & Bamboo Apps: Use YOURAPPNAME.heroku.com

Cedar Apps: Use YOURAPPNAME.herokuapp.com

Check that your DNS is configured correctly with the host command:

$ host www.mydomain.com
www.mydomain.com is an alias for warm-spring-23.heroku.com.
warm-spring-23.heroku.com is an alias for proxy.heroku.com.
proxy.heroku.com has address 75.101.163.44
proxy.heroku.com has address 75.101.145.87
proxy.heroku.com has address 174.129.212.2 

Output of the host command varies by Unix flavor, but it should indicate that your host name is either an alias or CNAME of YOURAPPNAME.heroku[app].com.

Wildcard Domains

If you’d like your app to respond to any subdomain under your custom domain name (as in *.yourdomain.com), you’ll need to use the Wildcard Domains add-on. First, select the add-on:

$ heroku addons:add custom_domains:wildcard
Adding custom_domains:wildcard to myapp...done.
$ curl http://arbitrary-subdomain.myapp.heroku.com/
HTTP/1.1 200 OK

To use with a custom domain, configure your registrar to point *.yourdomain.com at YOURAPPNAME.heroku[app].com. If things are set up correctly you should be able to look up any arbitrary subdomain:

$ host arbitrary-subdomain.yourdomain.com
arbitrary-subdomain.yourdomain.com is an alias for myapp.heroku.com.

Then add the wildcard domain on Heroku:

$ heroku domains:add *.yourdomain.com
Added *.yourdomain.com as a custom domain name to myapp.heroku.com

$ curl http://arbitrary-subdomain.yourdomain.com/
HTTP/1.1 200 OK

Redirecting Traffic to a Specific Domain

If you have multiple domains or your app has users that access it via its Heroku subdomain before you later switched to your own custom domain, you will probably want to get all users onto the same domain with a redirect.

Here’s an example of how to do it in Rails with a before filter - a similar approach should work in other languages and frameworks:

class ApplicationController
  before_filter :ensure_domain

  APP_DOMAIN = 'myapp.mydomain.com'

  def ensure_domain
    if request.env['HTTP_HOST'] != APP_DOMAIN
      # HTTP 301 is a "permanent" redirect
      redirect_to "http://#{APP_DOMAIN}", :status => 301
    end
  end
end

Frequently Asked Questions

What IP addresses should I use to point my custom domain to Heroku?

If you are pointing a root domain (e.g. example.org) at Heroku, add these three IP addresses as A records.

75.101.163.44
75.101.145.87
174.129.212.2

If you are pointing a subdomain (e.g. sub.example.org) at Heroku, point a CNAME record to <appname>.heroku[app].com. E.g., if your app is called example:

  • Aspen and bamboo apps: example.heroku.com
  • Cedar apps: example.herokuapp.com
Why can't I add subdomain.mydomain.com for my app?

In some cases, attempts to add a custom domain (like subdomain.mydomain.com) for an app may result in an error like this:

 ! mydomain.com is owned by another user

All applications for a given base domain must be owned by the same Heroku account. The above error means that someone else has already added a mydomain.com custom domain to one of their apps.