Rails routing to handle multiple domains on single application

Ruby on-RailsRuby on-Rails-3RoutingRoutesSubdomain

Ruby on-Rails Problem Overview


I've been unable to find a workable solution to this problem, despite several similar questions here and elsewhere. It seems likely that this question hasn't been answered for Rails 3, so here goes:

I have an application that currently allows users to create their own subdomain that contains their instance of the application. While in Rails 2 you were best served using the subdomain-fu gem, in version 3 it's dramatically simpler, as per the Railscast -- http://railscasts.com/episodes/221-subdomains-in-rails-3.

That's good stuff, but I also want to provide the option for users to associate their own domain name with their account. So while they might have http://userx.mydomain.com, I'd like them to choose to have http://userx.com associated as well.

I found a few references to doing this in Rails 2, but those techniques don't appear to work anymore (particularly this one: https://feefighters.com/blog/hosting-multiple-domains-from-a-single-rails-app/).

Can anyone recommend a way to use routes to accept an arbitrary domain and pass it along to a controller so I can show the appropriate content?

Update: I've gotten most of an answer now, thanks to Leonid's timely response, and a fresh look at the code. It ultimately required an addition to the existing Subdomain code that I was using (from the Railscast solution) and then adding a bit to routes.rb. I'm not all the way there yet but I want to post what I have so far.

In lib/subdomain.rb:

class Subdomain
  def self.matches?(request)
    request.subdomain.present? && request.subdomain != "www"
  end
end

class Domain
  def self.matches?(request)
    request.domain.present? && request.domain != "mydomain.com"
  end
end

I've added the second class in imitation of the first, which is known working. I simply add a condition that ensures that the incoming domain is not the one for which I'm hosting the main site.

This class is used in routes.rb:

require 'subdomain'
constraints(Domain) do
  match '/' => 'blogs#show'
end

constraints(Subdomain) do
  match '/' => 'blogs#show'
end

Here, I'm prepending the existing subdomain code (again, it's working fine) with a stanza to check for the Domain. If this server responds to that domain and it's not the one under which the main site operates, forward to the specified controller.

And while that appears to be working, I don't quite have the whole thing working yet, but I think this particular problem has been solved.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

It's actually simpler in Rails 3, as per http://guides.rubyonrails.org/routing.html#advanced-constraints:

  1. define a custom constraint class in lib/domain_constraint.rb:

    class DomainConstraint def initialize(domain) @domains = [domain].flatten end

    def matches?(request) @domains.include? request.domain end end

  2. use the class in your routes with the new block syntax

    constraints DomainConstraint.new('mydomain.com') do root :to => 'mydomain#index' end

    root :to => 'main#index'

or the old-fashioned option syntax

root :to => 'mydomain#index', :constraints => DomainConstraint.new('mydomain.com')

Solution 2 - Ruby on-Rails

In Rails 5, you can simply do this in your routes:

constraints subdomain: 'blogs' do
  match '/' => 'blogs#show'
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAaron VeghView Question on Stackoverflow
Solution 1 - Ruby on-RailsLeonid ShevtsovView Answer on Stackoverflow
Solution 2 - Ruby on-Railsuser3033467View Answer on Stackoverflow