如何从 url 获取子域值? [英] How to get the subdomain value from a url?

查看:47
本文介绍了如何从 url 获取子域值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 rails 中获取子域值,是否有内置方法可以做到这一点?

how can I get the subdomain value in rails, is there a built-in way to do this?

例如

test123.example.com

我想要网址的 test123 部分.

I want the test123 part of the url.

推荐答案

Rails 3.0 内置了此功能,您可以从 request.subdomain 访问子域.

Rails 3.0 has this capability built-in, you can access the subdomain from 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

如果您使用的是 2.3,则需要使用诸如 subdomain-fu.

If you're using 2.3, you'll need to use a plugin such as subdomain-fu.

这篇关于如何从 url 获取子域值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆