Take the 2-minute tour ×

I have looked all over the internet but could not get one final solution to what I would expect to be the 100% proper setup of DNS etc to get the following result on Heroku.com:

I can get a bit of help from everywhere but nothing conclusive that will take care of the whole problem from DNS to rack-rewrite so that is why I am asking here (the support at my registrar and Heroku can only give me "their part").

What needs to be done is, as far as I understand:

  1. Add mydomain.com and www.mydomain.com to Custom Domains at Heroku

  2. Change CNAME to http://myapp.herokuapp.com in my registrars DNS. Question: Would it be both "www" and "*" that would use the CNAME redirection to myapp.herokuapp.com?

  3. Add a "@" DNS record in order to handle naked domain requests (http://mydomain.com). Since this will not be done with CNAME, I am using the IP numbers at https://devcenter.heroku.com/articles/custom-domains, although they have severe uptime problems according to the article.

-- 1-3 leaves me with a website that will serve both http://mydomain.com and http://www.mydomain.com with the content at http://myapp.herokuapp.com

c. Now, I want to redirect everything from http://mydomain.com to http://www.mydomain.com and, according to Heroku support, this should be done with rack-rewrite (https://github.com/jtrupiano/rack-rewrite). Using the example in there, the code should then be:

r301 %r{.*}, 'http://mydomain.com$&', :if => Proc.new {|rack_env|
  rack_env['SERVER_NAME'] != 'www.mydomain.com'

This, however, creates an eternal loop for me.

So, my question is now: What would be the proper workflow for Heroku and DNS-settings in order to achieve this setup?

It is very difficult to troubleshoot this, since trying out different DNS-settings by trial and error is very difficult (it's hard to know if they have gone through).

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

It's the rewrite rule I think that it is saying "do a 301 redirect to 'http://mydomain.com$&' (where the $& indicates that it will keep everything after the .com) but only if the host in the request is not 'www.mydomain.com'".

So you'll go into a redirect loop because any request to your server that doesn't have the www in it, e.g. the naked mydomain.com, will redirect back to mydomain.com.

It probably needs rewriting to something like:

r301 %r{.*}, 'http://www.mydomain.com$&', :if => Proc.new {|rack_env|
rack_env['SERVER_NAME'] == 'mydomain.com'}

So you are redirecting to the www version of your domain.

Actually there is a dupe here

Edit: This is what I would do:

  1. remove your redirect rules
  2. set up your dns so that both www.domain.com and domain.com returns myapp.herokuapp.com
  3. verify that the dns works without worrying about redirection
  4. make sure that at the DNS level it isn't trying to do some helpful redirecting

At this stage there isn't anything else to do with the DNS. The only thing they do is return an IP address (A record) or another domain (CNAME). Then their job is done.


  1. Apply the rack-rewrite rule
  2. The server might need a restart - do it just in case
  3. Test using something like Sam Spade - the headers will contain a 301 redirect and the new location if it has worked properly
share|improve this answer
Thanks, that shines some light on the redirect issue. Regarding the duplicate, I have tested that one as well but maybe because of the DNS settings I had, I couldn't make it work. That is why I want to take a "complete" approach to it since the DNS settings will affect this as well, and it is difficult (for me) to understand what is doing what. –  Christoffer May 1 '12 at 9:20
...oh, and that will actually create an endless loop for me as well. Don't know if it is because of my DNS setting but doesn't work nonetheless. –  Christoffer May 1 '12 at 9:30
use tools like Sam Spade, Fiddler and your basic ping to troubleshoot whether the issue is with DNS or your web app. If DNS is returning the appropriate domain/ip then it probably isn't a DNS issue. –  edralph May 1 '12 at 11:57
I needed to use "www" at add rack_env['SERVER_NAME'] == 'www.mydomain.com' but that made it work. Thanks. –  Christoffer May 8 '12 at 20:18
add comment

Your Answer


