I could not find a workable solution to this problem, despite several similar questions here and elsewhere. It seems likely that there was no answer to this question for Rails 3, so like this:
I have an application that currently allows users to create their own subdomain that contains their application instance. While in Rails 2 you are best off using the subdomain-fu gem, in version 3 it is much simpler than in Railscast - http://railscasts.com/episodes/221-subdomains-in-rails-3 .
This is a good thing, but I also want to give users the ability to associate their domain name with an account. Therefore, although they may have http://userx.mydomain.com , I would like them to link http://userx.com too.
I found some links to this in Rails 2, but these methods no longer work (especially 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 to the controller so that I can show the relevant content?
Update : I got most of the answers now thanks to Leonid's timely response and a new look at the code. Ultimately, this required an addition to the existing subdomain code that I used (from the Railscast solution), and then added a bit to rout.rb. I have not reached yet, but I want to publish what I have.
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 added a second class to imitate the first, which, as you know, works. I just add a condition to ensure that the inbound domain is not the one for which I host the main site.
This class is used in rout.rb:
require 'subdomain' constraints(Domain) do match '/' => 'blogs#show' end constraints(Subdomain) do match '/' => 'blogs#show' end
Here I am supplementing the existing subdomain code (again, it works fine) with a stanza to check the domain. If this server responds to this domain, and not the one under which the main site runs, go to the specified controller.
And although this seems to work, not everything works for me yet, but I think this specific problem has been resolved.