Rails: Proper setup with DNS, Rack-rewrite etc for Heroku

こ雲淡風輕ζ 提交于 2019-12-04 15:11:22
edralph

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.

Next:

  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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!