Host and domain based routing in Ruby on Rails

Jonathon Horsman Jan 20 13 comments

I spent some time trying to figure out how to have host-based routing in our upcoming Ruby on Rails application.

The Ruby Gem subdomain-fu almost gave me what I wanted, except it only allowed routing based on sub-domain rather than the entire host name in the URL.

The purpose is to allow our clients to have any domain point to our server, and correctly determine what to render based on a database match of the domain.

To achieve this I forked subdomain-fu, and simply added 2 lines to routing_extensions.rb:

    def recognition_conditions_with_subdomain
      result = recognition_conditions_without_subdomain
      result << "conditions[:subdomain] === env[:subdomain]" if conditions[:subdomain] && conditions[:subdomain] != true && conditions[:subdomain] != false
      result << "SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == true
      result << "!SubdomainFu.has_subdomain?(env[:subdomain])" if conditions[:subdomain] == false

      # added these 2 lines:
      result << "conditions[:domain] === env[:domain]" if conditions[:domain]
      result << "conditions[:host] === env[:host]" if conditions[:host]

I’m not clear why these weren’t added in the subdomain-fu gem, since the detection of domain and host are already stored in the env array.

Then modify the routes.rb file to include the new conditions:

ActionController::Routing::Routes.draw do |map|

  map.connect "", :controller => "home", :conditions => { :host => "" }
  map.connect "", :controller => "listings"
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'

As the routes are checked sequentially for a match, if isn’t matched then we fall through to the second map.connect entry which is a catch all for any other domain.

The difference between host and domain is the domain is the part after the subdomain (e.g., whereas the host is the subdomain and domain (e.g.

See the original subdomain-fu gem for more details
My forked gem is available on Github
And Railscasts has a useful vid on the topic

Comments //


Joshua Feb 01

thanks for this, exactly what I needed - would be great if this could be merged back into subdomain-fu


Ramon May 01

Yeah, this should be merged into subdomain fu! I was trying to get subdomain-fu and this code ( to work, but it seems they didn't work nicely together. Didn't know where to look. Glad I found your page!


Dylan Aug 08

Thanks for posting this, it's just what I'm looking for.

So each user who has their own domain must be declared in the routing? Is there a way to make that part dynamic? I'm looking for a solution that would scale well.


Jonathon Horsman

Jonathon Horsman Aug 09

Hi Dylan

The third line of the routes.rb file in the example:

map.connect "", :controller => "listings"

will route all requests to "/" to the listings controller, regardless of the subdomain.
So no, you don't need to declare every subdomain in routes.rb. You will want to modify your apache config to include something like this:

ServerAlias *

Inside your controller you can determine the subdomain using the 'subdomains' method.

Note that Rails 3 has built-in subdomain support (which I haven't tried yet) but may mean you don't have to bother with any of this.



Dylan Aug 13

Thanks for the response Jonathan, that cleared it up for me.


Ramon Tayag

Ramon Tayag Oct 22

It'd be awesome if this could be used with Rails3!

Jonathon Horsman

Jonathon Horsman Oct 22

Hey Ramon, you shouldn't need this in Rails 3 because it's baked in, check out the Railscast:

Pieter Eerlings

Pieter Eerlings Nov 07

Great, this is what we needed. Should be forked back into subdomain-fu master branch!


Amit Jul 29

Great Solutions

I am looking for multiple Domain and Multiple Sub domains (for One Domain and multiple Domains its woks for me).

Example -



and more

Can you please help me on this?



Smith Jun 11

Hi Amit,

Could you please tell me how to create the One Domain website in rails3

can u send me the code and instructions

Smith Oct 30

I like the helpful information you provide in your articles.
I'll bookmark your weblog and check again here regularly.
I'm quite sure I'll learn many new stuff right here!

Best of luck for the next!

plumbing companies ft lauderdale

plumbing companies ft lauderdale Dec 17

Nicе аnѕwer back in rеturn off this query with fіrm arguments and
explaining everything concerning that.


Geri Feb 08

First ooff I would like to say awesome blog! I had a quick question which
I'd like to ask if you don't mind. I wwas interested
to find out how you center yourself and cclear your thoughts
before writing. I've had difficculty clearing my mind in getting my ideas out.
I truly do enjoy writing however it just seems like the first 10to
15 minutes tend to be wasted just trying to figure out how
to begin. Any recommendations or tips? Many thanks!

Post a comment

  1. optional

Recent Tweets

Blog: The monumental Myspace cock-up:
Tweeted on Friday at 09:43

Awww railsapi, delete some logs:
Tweeted on Wednesday at 16:15