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:
Add mydomain.com and www.mydomain.com to Custom Domains at Heroku
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?
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).
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:
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.
Next: