【问题标题】:redirect 301 subdomains to users own urls in rails 3在 rails 3 中将 301 个子域重定向到用户自己的 url
【发布时间】:2011-04-18 14:21:00
【问题描述】:

我有一个 Rails 应用程序,它为用户提供了他们网站的子域。然后,用户可以将自己的域指向该子域,使其看起来像他们自己的站点。

当我的用户在我的应用程序中输入域名时,我需要将任何未来的请求重定向到我的应用程序上的子域 url 到他们自己的域,同时传递路径,例如 company_site.hosted_site.com/pages/about_us 重定向到 www。 company_site.com/pages/about_us。

有谁知道如何在 rails 3 中做到这一点?

我不能因此使用 apache 规则,因为需要访问数据库以获取重定向 url,并且也不想更改每个用户/子域的 conf 文件。

有人做过吗? ssems 最好在机架中间件中完成?有什么想法吗?

非常感谢 瑞克

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 redirect


    【解决方案1】:

    一个简单的方法是在 ApplicationController 的 before_filter 块中执行此操作,以便所有操作都受其影响:

    before_filter :redirect_to_custom_domain
    
    def redirect_to_custom_domain
      if (customer = Customer.find_by_subdomain(request.host))
        if (customer.domain?)
          # Redirects to the customer's full domain
          redirect_to(customer.domain_url)
    
          # Returning false will halt additional processing for this request
          return false
        end
      end
    end
    

    客户将是您存储分配的subdomain 和一个可选的自定义domain 的记录,如果指定,它们将被重定向到。

    在您的客户记录中,您可能有这样的帮助方法:

    def Customer < ActiveRecord::Base
      def domain_url
        "http://#{domain}/"
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2013-09-26
      • 1970-01-01
      • 2012-01-19
      • 2016-07-30
      • 2017-06-15
      • 2011-07-03
      • 1970-01-01
      • 2014-12-04
      • 2011-07-08
      相关资源
      最近更新 更多