【问题标题】:How to create dynamic subdomain rails如何创建动态子域轨道
【发布时间】:2015-12-14 10:34:46
【问题描述】:

如何为在我的网站上注册的每个用户创建子域?例如 userone.mysite.com 和 usertwo.mysite.com。

在 php 中可以使用 apache 虚拟主机来完成,但我不知道如何在 Ruby on Rails 中做同样的事情。这是如何在 apache 中完成的

<VirtualHost *:80>
    ServerName www.mysite.com
    ServerAlias mysite.com *.mysite.com
    DocumentRoot /www/domain
</VirtualHost>

我浏览了许多博客,但找不到解决方案。 请指教。

【问题讨论】:

  • 你需要物理子域还是虚拟域
  • 我需要虚拟域

标签: ruby-on-rails ruby url-routing


【解决方案1】:
 <VirtualHost *:80>
      ServerName mysite.com
      ServerAlias *.my_site.com
      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /var/www/html/my_site
      <Directory /var/www/html/my_site>
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options -MultiViews
         # Uncomment this if you're on Apache >= 2.4:
         Require all granted
      </Directory>
   </VirtualHost>

不要忘记在 DNS 中将 cname * 条目更改为您的域

【讨论】:

  • 请告诉我hostintg提供者,以便我可以为您提供准确的cname配置
【解决方案2】:

除了 Dinesh Saini 答案 - 您还需要相应地更新 Rails 配置。例如,如果您需要深层子域,您应该更改 staging.rb 中的 config.action_dispatch.tld_length,你应该重新检查 routes.rb。

实时示例:我必须通过子域实现商店显示 - 示例 URL my-shop.shop.testapp.com.所以除了服务器配置改变我做了什么

   constraints (lambda { |req| req.subdomains[1] == 'shop' }) do
      get '/', to: 'shopes#show', as: :shop
   end

在控制器中查找资源

   Shop.find_by(id: request.subdomains[0])

我也设置了

   config.action_dispatch.tld_length = 2

我这样做是为了 staging env,因为它有这样的 URL staging.testapp.com,所以我需要另一个子域级别。我认为对你来说,最好检查它是否有 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-14
    • 2022-01-16
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 2012-08-11
    • 1970-01-01
    相关资源
    最近更新 更多