【发布时间】:2016-06-21 14:31:06
【问题描述】:
我在 heroku 上部署了一个 rails 应用程序,Google 的域和 cloudflare 的 DNS。我想将所有来自example.herokuapp.com 的请求重定向到example.com。我不能在 cloudflare 上设置这个,因为如果我使用一个页面规则将流量从 example.herokuapp.com 发送到 example.com 那么 cloudflare 会引发这个错误:ERROR Your URL should reference the domain 'example.com' in some way.
我也检查了heroku,但我真的不明白我应该做什么:
“即使您设置了自定义域,您的应用程序的 Heroku 域也将始终保持活动状态。如果您希望用户独占使用自定义域,您的应用程序应发送 HTTP 状态 301 Moved Permanently 以告知 Web 浏览器使用自定义域。Host HTTP 请求标头字段将显示用户尝试访问的域;如果该字段是 example.herokuapp.com,则发送重定向。"
将流量重定向到https://example.com 的首选方式是什么?
更新
应用程序.rb
config.middleware.insert_before(Rack::Runtime, Rack::Rewrite) do
r301 %r{.*}, "https://example.com$&",
:if => Proc.new { |rack_env| rack_env['SERVER_NAME'] != 'example.com' }
end
【问题讨论】:
标签: ruby-on-rails heroku dns cloudflare