smallroomsoftware.com

Rails routing based on hostname

Posted on February 10, 2007

Matching a route based on hostname is possible with a few patches to the rails routing code. I followed the advice on Jamis Buck's weblog and patched two methods from my environment.rb:

module ActionController
  module Routing
    class RouteSet
      def extract_request_environment(request)
        { :method => request.method, :hostname => request.domain.split('.').first }
      end
    end
    class Route
      alias_method :old_recognition_conditions, :recognition_conditions
      def recognition_conditions
        result = old_recognition_conditions
        result << "conditions[:hostname] === env[:hostname]" if conditions[:hostname]
        result
      end
    end
  end
end

You can then write routes like:

map.connect '', :controller => 'blah', :action => 'blah', :conditions => {:hostname => 'blah'}

Obviously, you can use a regular expression for the condition argument if you like.

Comments
  1. EnricoApril 30, 2007 @ 01:56 PM
    I'm a rails newbie and I tried your suggestion and I can get it to work in the following case: map.home '', :controller => 'home', :action => 'index' , :conditions => {:hostname => 'foo'} but this one is not working: map.resources :users, :conditions => {:hostname => 'foo'} do |user| user.resources :avatar end Is there something I'm missing?
  2. JerryAugust 10, 2007 @ 06:42 PM
    After getting a nil.split error when using an IP address, I doctored it up like this: def extract_request_environment(request) domain = request.domain.nil? ? '' : request.domain { :method => request.method, :hostname => domain.split('.').first } end
  3. kikMay 12, 2008 @ 09:29 PM
    That's a very nice tip, thanks a lot
  4. kikFebruary 15, 2009 @ 10:19 PM
    Just to mention a way to get this running with rspec, if you still use that method. It appears that rspec will fail to recognize the routes based on a hostname conditions since it calls recognize_path without :hostname : http://rspec.rubyforge.org/rspec-rails/1.1.12/classes/Spec/Rails/Example/ControllerExampleGroup.src/M000068.html I managed to get rid of this by putting in spec/spec_helper.rb : module Spec module Rails module Example class ControllerExampleGroup def params_from( method, path, hostname=request.host ) ensure_that_routes_are_loaded ActionController::Routing::Routes.recognize_path(path, :method => method, :hostname => hostname ) end end end end end
  5. kikFebruary 15, 2009 @ 10:21 PM
    woops, here is a pastebin of the code : http://pastie.org/390082
  6. Bill BurchamApril 11, 2009 @ 05:33 PM
    How do you test an application that has hooked extract_request_environment? I find that at least rspec does not cause the hook to be called when confirming redirects (ticket here https://rspec.lighthouseapp.com/projects/5645/tickets/538-redirect_to-should-work-with-extended-request-environment
  7. DevenSeptember 04, 2009 @ 10:37 AM
    hello dear, I have the same redirect problem in ror. actualy i have 2 application in same project admin and client. with diff namespace and i have 2 diff url so i have set root url based on this map.root :namespace => "admin", :controller => "home", :action => "index", :conditions => {:hostname => 'admin.ror.dk'} map.root :namespace => "client", :controller => "client", :action => "index", :conditions => {:hostname => 'client.ror.dk'} but it's not work it's redirect every time on first root url admin.ror.dk i have this set code in envirment.rb also module ActionController module Routing class RouteSet def extract_request_environment(request) { :method => request.method, :hostname => request.domain.split('.').first } end end class Route alias_method :old_recognition_conditions, :recognition_conditions def recognition_conditions result = old_recognition_conditions result << "conditions[:hostname] === env[:hostname]" if conditions[:hostname] result end end end end but then also it's redirect on first root url.. if u have the solution then give me... thank Deven Patel
  8. Stefan BorsjeNovember 05, 2009 @ 11:05 PM
    This didn't work for me anymore, so I've modified this one a little. You can find it here: http://gist.github.com/227473 Notice that I use a slightly different approach by using the ':host' instead of the first part of the domain.
  9. starfryDecember 12, 2009 @ 10:44 PM
    Stefan you did not include the "extract_request_environment" in your code. Why was this? Is it not needed? It does not seem to work without it ?
Post a comment
Comment



If you can read this, you don't use a typical webbrowser that plays nice with CSS.
Please do not fill in anything here!



Hosting by site5.com