Custom domain names
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
The first step is to tell Heroku to respond to requests at your domain. This requires adding the custom_domain addon, and telling Heroku what domain to respond to.
| 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 |
$ heroku addons:add custom_domains
Adding wildcard_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:
mydomain.com
Root domains must use A records. Only your root domain (mydomain.com) may 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
host.mydomain.com (e.g. 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 proxy.heroku.com. In DNS terms this is known as a CNAME.
Check that your DNS is configured correctly with the host command:
$ host www.mydomain.com
www.mydomain.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 proxy.heroku.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 wildcard_domains
Adding wildcard_domains 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 heroku.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 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 Specific Domain
If you have multiple domains, or your app has users that access it via its Heroku subdomain but you later switched to your own custom domain, you will probably want to get all users onto the same domain with a redirect in a before filter. Something like this will do the job:
class ApplicationController
before_filter :ensure_domain
TheDomain = 'myapp.mydomain.com'
def ensure_domain
if request.env['HTTP_HOST'] != TheDomain
redirect_to TheDomain
end
end
end