Ruby-on-rails 如何从 url 获取子域值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4310547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the subdomain value from a url?
提问by Blankman
how can I get the subdomain value in rails, is there a built-in way to do this?
如何在 rails 中获取子域值,是否有内置方法可以做到这一点?
e.g.
例如
test123.example.com
I want the test123 part of the url.
我想要 url 的 test123 部分。
回答by Adam Lassek
Rails 3.0 has this capability built-in, you can access the subdomain from request.subdomain.
Rails 3.0 内置了此功能,您可以从request.subdomain.
You can also route based on the subdomain:
您还可以根据子域进行路由:
class SupportSubdomain
def self.matches?(request)
request.subdomain == "support"
end
end
Basecamp::Application.routes do
constraints(SupportSubdomain) do
match "/foo/bar", :to => "foo#bar"
end
end
If you're using 2.3, you'll need to use a plugin such as subdomain-fu.
如果您使用的是 2.3,则需要使用诸如subdomain-fu 之类的插件。
回答by Weston Ganger
Use the following method inside your controller
在控制器中使用以下方法
request.subdomains
This Returns an array of subdomains
这将返回子域数组
回答by Nimesh Nikum
account_locationis also a good plugin. After using it, you can find the account based on different subdomains. And you can find out subdomain from url just by writing
account_location也是一个不错的插件。使用后,您可以根据不同的子域查找帐户。你可以通过编写从 url 中找到子域
request.subdomains(0).first 在你的代码中。回答by Joshua
In case you are working with a string, and assuming it can be a true URI, you can do this to extract the subdomain.
如果您正在处理一个字符串,并假设它可以是一个真正的 URI,您可以执行此操作来提取子域。
require 'uri'
uri = URI.parse('http://test123.example.com')
uri.host.split('.').first
=> "test123"
回答by Rick
Simple in your controller just do the following
在您的控制器中简单只需执行以下操作
unless request.subdomains.any?
#No domains available redirect
redirect_to subdomain: 'www'
end
回答by Matt Fikowski
For anyone looking to get the subdomains on localhost using WEBrick:
对于希望使用 WEBrick 在本地主机上获取子域的任何人:
Put config.action_dispatch.tld_length = 0into config/environments/development.rband everything should work.
把config.action_dispatch.tld_length = 0到config/environments/development.rb,一切都应该工作。
Link to SO post here: Can I make Rails / WEBrick recognize entries in /etc/hosts as subdomains (instead of domains)?
链接到此处的 SO 帖子: 我可以让 Rails / WEBrick 将 /etc/hosts 中的条目识别为子域(而不是域)吗?
Link to Github post: https://github.com/rails/rails/issues/12438
Github 帖子链接:https: //github.com/rails/rails/issues/12438
回答by Rohit
You can use the SubdomainFu plugin. This plugin gives you a method current_subdomain which returns the current_subdomain of your app.
您可以使用SubdomainFu 插件。这个插件为你提供了一个方法 current_subdomain ,它返回你的应用程序的 current_subdomain。
You can also have a look at this Railscast
你也可以看看这个 Railscast
UPDATE
更新
You can also use request.subdomainsthis will give you an array of subdomains.
您也可以使用request.subdomains它为您提供一系列子域。
回答by Andrew Shatnyy
A bit late to the party but here's what I used in older versions of rails.
聚会有点晚了,但这是我在旧版本的 rails 中使用的内容。
subdomain = request.subdomains.join('.')
subdomain = request.subdomains.join('.')
It should be backwards compatible in newer versions
它应该在新版本中向后兼容

