【发布时间】:2019-06-13 05:13:00
【问题描述】:
根据我对其他问题的阅读,我觉得这应该是可行的,但到目前为止我还没有发现任何东西这样做。我有一个应用程序可以让俱乐部运动队托管一个网站(理论上)。使用 lvh.me 时,在我的本地机器上一切正常;但是部署到 heroku 会破坏路由。
Ruby on rails 5.1
到目前为止我已采取的步骤:
将子域添加到 heroku 域。对于 heroku 应用程序,我现在将根域作为 ALIAS 或 ANAME,将 www 作为 cname,并将新添加的子域 (pincity) 作为 cname。
将 cname 添加到我的 dns 提供程序。使用 dig 返回子域返回正确的 {crazy-heroku-name}.herokudns.com 地址
在部署并将这个团队添加到生产应用程序数据库('pincity' 的 slug,以便 pincity.mydomain.com 工作)之后,我重新启动了 dynos。
我认为这应该是我需要做的。
我的路线文件相当简单
class TeamWebsiteConstraint
def matches?(request)
Rails.logger.info "subdomain is #{request.subdomain}"
Team.where(slug: request.subdomain).any?
end
end
Rails.application.routes.draw do
# all other routes
# Club team custom websites
constraints TeamWebsiteConstraint.new do
root 'team_website#home', as: :team_website_root
get 'about', to: 'team_website#about', as: :team_website_about
get 'schedule', to: 'team_website#schedule', as: :team_website_schedule
get 'faqs', to: 'team_website#resources', as: :team_website_resources
get 'contact', to: 'team_website#contact', as: :team_website_contact
end
root 'marketing#home'
end
现在去heroku时,子域将我重定向到根域。输入 pincity.mydomain.com/about 会导致 404
编辑: 我在 teamconstraint 路由中添加了一些日志记录。这是日志中的一些内容。
at=info method=GET path="/about" host=pincity.wrestlingiq.com request_id=b004b8cc-08e4-4bc9-a87a-d4b37deaa29c fwd="71.202.0.175" dyno=web.1 connect=1ms service=4ms status=301 bytes=391 protocol=https
subdomain is www
似乎 heroku 路由器在处理路由代码之前进行了 301 重定向,这意味着子域约束永远没有机会触发。
编辑 2: 我在 DNSSimple 中发现了一条将根域重定向到 www 版本的 URL 记录。我已经删除了它,并添加了应用程序逻辑来处理该重定向。希望有帮助。
【问题讨论】:
标签: ruby-on-rails ruby heroku routing subdomain