Ruby on rails 如何允许客户子域站点使用自己的域
我有一个rails应用程序,客户可以在其中注册并获得自定义子域站点/url,如下所示:Ruby on rails 如何允许客户子域站点使用自己的域,ruby-on-rails,dns,subdomain,Ruby On Rails,Dns,Subdomain,我有一个rails应用程序,客户可以在其中注册并获得自定义子域站点/url,如下所示: before_filter :load_customer_from_host def load_customer_from_host # Strip "www." from host name to search only by domain. hostname = request.host.sub(/^www\./, '') @customer = Customer.find_by_host
before_filter :load_customer_from_host
def load_customer_from_host
# Strip "www." from host name to search only by domain.
hostname = request.host.sub(/^www\./, '')
@customer = Customer.find_by_host!(hostname)
rescue ActiveRecord::RecordNotFound
render(:partial => 'customer_not_found', :layout => 'application', :status => :not_found)
end
- customer1.myapp.com
- customer2.myapp.com
- customer3.myapp.com
谢谢。您的客户需要为他们的域设置DNS,以将其或其一部分指向您的地址。这可能很难协调,尤其是当您托管服务的服务器的地址可能会不时更改时。将客户的子域路由到您的子域要容易得多 您还需要一个将客户的域映射到客户帐户的查找表。这通常表示为以下内容:
before_filter :load_customer_from_host
def load_customer_from_host
# Strip "www." from host name to search only by domain.
hostname = request.host.sub(/^www\./, '')
@customer = Customer.find_by_host!(hostname)
rescue ActiveRecord::RecordNotFound
render(:partial => 'customer_not_found', :layout => 'application', :status => :not_found)
end
这假定您有一个客户模型,其中的“主机”字段设置为“customer1.myapp.com”或“customer1.com”,只要与主机字段匹配即可
设置应用程序时,需要有一个虚拟主机配置,以响应所有任意域名。如果它是唯一托管的站点,这很容易做到,因为这是默认行为。如果您在共享主机上执行此操作,则可能需要为每个客户域专门配置一个别名,这可能会造成麻烦