Rails 路由可在单个应用程序上处理多个域

发布于 2024-10-03 04:24:30 字数 1683 浏览 2 评论 0原文

尽管这里和其他地方有几个类似的问题,但我一直无法找到解决此问题的可行解决方案。 Rails 3 似乎还没有回答这个问题,所以这里是:

我有一个应用程序,当前允许用户创建自己的包含应用程序实例的子域。虽然在 Rails 2 中,您最好使用 subdomain-fu gem,但在版本 3 中,它要简单得多,根据 Railscast -- http://railscasts.com/episodes/221-subdomains-in-rails-3

这是个好东西,但我还想为用户提供将自己的域名与其帐户关联的选项。因此,虽然他们可能有 http://userx.mydomain.com,但我希望他们选择 < a href="http://userx.com" rel="noreferrer">http://userx.com 也关联。

我发现了一些在 Rails 2 中执行此操作的参考,但这些技术似乎不再起作用(特别是这个:https://feefighters.com/blog/hosting-multiple-domains-from-a-single-rails-app/)。

任何人都可以推荐一种使用路由接受任意域并将其传递给控制器​​以便我可以显示适当内容的方法吗?

更新:由于 Leonid 的及时回复以及对代码的重新审视,我现在已经得到了大部分答案。它最终需要添加到我正在使用的现有子域代码(来自 Railscast 解决方案),然后添加一些到 paths.rb。我还没有完全做到这一点,但我想发布我到目前为止所拥有的。

在 lib/subdomain.rb 中:

class Subdomain
  def self.matches?(request)
    request.subdomain.present? && request.subdomain != "www"
  end
end

class Domain
  def self.matches?(request)
    request.domain.present? && request.domain != "mydomain.com"
  end
end

我添加了第二个类来模仿第一个类,这是已知的工作原理。我只是添加一个条件,确保传入域不是我托管主站点的域。

这个类在routes.rb中使用:

require 'subdomain'
constraints(Domain) do
  match '/' => 'blogs#show'
end

constraints(Subdomain) do
  match '/' => 'blogs#show'
end

在这里,我在现有的子域代码(同样,它工作正常)前面加上一个节来检查域。如果该服务器响应该域,但它不是主站点运行的域,则转发到指定的控制器。

虽然这似乎有效,但我还没有完全解决整个问题,但我认为这个特殊问题已经解决了。

I've been unable to find a workable solution to this problem, despite several similar questions here and elsewhere. It seems likely that this question hasn't been answered for Rails 3, so here goes:

I have an application that currently allows users to create their own subdomain that contains their instance of the application. While in Rails 2 you were best served using the subdomain-fu gem, in version 3 it's dramatically simpler, as per the Railscast -- http://railscasts.com/episodes/221-subdomains-in-rails-3.

That's good stuff, but I also want to provide the option for users to associate their own domain name with their account. So while they might have http://userx.mydomain.com, I'd like them to choose to have http://userx.com associated as well.

I found a few references to doing this in Rails 2, but those techniques don't appear to work anymore (particularly this one: https://feefighters.com/blog/hosting-multiple-domains-from-a-single-rails-app/).

Can anyone recommend a way to use routes to accept an arbitrary domain and pass it along to a controller so I can show the appropriate content?

Update: I've gotten most of an answer now, thanks to Leonid's timely response, and a fresh look at the code. It ultimately required an addition to the existing Subdomain code that I was using (from the Railscast solution) and then adding a bit to routes.rb. I'm not all the way there yet but I want to post what I have so far.

In lib/subdomain.rb:

class Subdomain
  def self.matches?(request)
    request.subdomain.present? && request.subdomain != "www"
  end
end

class Domain
  def self.matches?(request)
    request.domain.present? && request.domain != "mydomain.com"
  end
end

I've added the second class in imitation of the first, which is known working. I simply add a condition that ensures that the incoming domain is not the one for which I'm hosting the main site.

This class is used in routes.rb:

require 'subdomain'
constraints(Domain) do
  match '/' => 'blogs#show'
end

constraints(Subdomain) do
  match '/' => 'blogs#show'
end

Here, I'm prepending the existing subdomain code (again, it's working fine) with a stanza to check for the Domain. If this server responds to that domain and it's not the one under which the main site operates, forward to the specified controller.

And while that appears to be working, I don't quite have the whole thing working yet, but I think this particular problem has been solved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的他你的她 2024-10-10 04:24:30

实际上,Rails 3 中更简单,按照 http://guides.rubyonrails.org/routing.html#advanced-constraints

1)在lib/domain_constraint.rb中定义自定义约束类:

class DomainConstraint
  def initialize(domain)
    @domains = [domain].flatten
  end

  def matches?(request)
    @domains.include? request.domain
  end
end

的类

constraints DomainConstraint.new('mydomain.com') do
  root :to => 'mydomain#index'
end

root :to => 'main#index'

2)在路由中使用新块语法或旧式选项语法

root :to => 'mydomain#index', :constraints => DomainConstraint.new('mydomain.com')

It's actually simpler in Rails 3, as per http://guides.rubyonrails.org/routing.html#advanced-constraints:

1) define a custom constraint class in lib/domain_constraint.rb:

class DomainConstraint
  def initialize(domain)
    @domains = [domain].flatten
  end

  def matches?(request)
    @domains.include? request.domain
  end
end

2) use the class in your routes with the new block syntax

constraints DomainConstraint.new('mydomain.com') do
  root :to => 'mydomain#index'
end

root :to => 'main#index'

or the old-fashioned option syntax

root :to => 'mydomain#index', :constraints => DomainConstraint.new('mydomain.com')
七色彩虹 2024-10-10 04:24:30

在 Rails 5 中,您可以在路线中简单地执行此操作:

constraints subdomain: 'blogs' do
  match '/' => 'blogs#show'
end

In Rails 5, you can simply do this in your routes:

constraints subdomain: 'blogs' do
  match '/' => 'blogs#show'
end
戏剧牡丹亭 2024-10-10 04:24:30

要扩展@user3033467答案,您不限于子域。

根据文档:

对于我的具体情况,我需要“域”和“子域”的约束”。

所以这对我有用:

constraints host: %w[domain.com] do

end

对于子域:

constraints subdomain: %w[subdomain www-subdomain] do

end

您甚至可以指定端口。

constraints subdomain: %w[subdomain www-subdomain], ports: 3000 do

end

为了将来的参考,这里是您可以传入的完整列表(单击查看完整大小):

在此处输入图像描述

To extend @user3033467 answer, you are not limited to subdomain.

As per the docs:

For my specific case, I need both constraint for "domain" and "subdomain".

So this works for me:

constraints host: %w[domain.com] do

end

For subdomain:

constraints subdomain: %w[subdomain www-subdomain] do

end

You can even go as far as specifying the ports too.

constraints subdomain: %w[subdomain www-subdomain], ports: 3000 do

end

For future reference, here's the full list you can pass in (click for full size):

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文