Blog

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]
      result
    end

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 => "www.mysite.com" }
  map.connect "", :controller => "listings"
  
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

As the routes are checked sequentially for a match, if www.mysite.com 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. mysite.com), whereas the host is the subdomain and domain (e.g. www.mysite.com).

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

Joshua Feb 01

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

Ramon

Ramon May 01

Yeah, this should be merged into subdomain fu! I was trying to get subdomain-fu and this code (http://transfs.com/devblog/2009/01/21/hosting-multiple-domains-from-a-single-rails-app/) to work, but it seems they didn't work nicely together. Didn't know where to look. Glad I found your page!

Dylan

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.

Thanks

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 *.mysite.com

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.

Cheers

Dylan

Dylan Aug 13

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

Cheers

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: http://railscasts.com/episodes/221-subdomains-in-rails-3

Pieter Eerlings

Pieter Eerlings Nov 07

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

Amit

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 -

1.
http://www.ror.com
http://www.ruby.ror.com
http://www.rails.ror.com

2.
http://www.php.com
http://www.cakephp.php.com
http://www.jquery.php.com

and more

Can you please help me on this?

Thanks
Amit

Smith

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

thanks,
Smith

selfassistrail.com

selfassistrail.com 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

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: http://bit.ly/emgRKV
Tweeted on Friday at 09:43

Awww railsapi, delete some logs: http://bit.ly/htBNDH
Tweeted on Wednesday at 16:15