Nathaniel S. H. Brown
2005-Dec-30 13:00 UTC
Unable to get the domain in my routes.rb to map custom route tables per domain
I have tried all night trying to find a way to get the host name which is currently being used within my routes.rb file to do some case/when switching for specific domains such as this cgi = ActionController::CgiRequest.new(CGI.new) case cgi.domain(1) when /www/ map.connect '''', :controller => ''public'', :action => ''index'' when /internal/ map.connect '''', :controller => ''admin'', :action => ''index'' end And have had no success. The domain(1) is empty in this case. I have tried a similar approach in one of my views, using the @request.domain(1) and that works PERFECT. Yet I have found no way to access the same @request.domain(1) within my routes.rb as the @request object has not been loaded yet. One thought was to use the ENV[''HTTP_HOST''] variable, but with my current LIGHTTPD setup, I do not have this environment variable around. Any help would be greatly appreciated. Regards, Nathaniel. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Nathaniel S. H. Brown http://nshb.net ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nathaniel S. H. Brown
2005-Dec-31 07:12 UTC
[Rails] Unable to get the domain in my routes.rb to map custom route tables per domain
I am still struggling with this. Anyone able to point me in the right direction? -Nb ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Nathaniel S. H. Brown http://nshb.net ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > Nathaniel S. H. Brown > Sent: December 30, 2005 5:01 AM > To: rails@lists.rubyonrails.org > Subject: [Rails] Unable to get the domain in my routes.rb to > map custom route tables per domain > > I have tried all night trying to find a way to get the host > name which is currently being used within my routes.rb file > to do some case/when switching for specific domains such as this > > cgi = ActionController::CgiRequest.new(CGI.new) > case cgi.domain(1) > when /www/ > map.connect '''', :controller => > ''public'', :action => ''index'' > when /internal/ > map.connect '''', :controller => ''admin'', > :action => ''index'' > end > > And have had no success. The domain(1) is empty in this case. > > I have tried a similar approach in one of my views, using the > @request.domain(1) and that works PERFECT. Yet I have found > no way to access the same @request.domain(1) within my > routes.rb as the @request object has not been loaded yet. > > One thought was to use the ENV[''HTTP_HOST''] variable, but > with my current LIGHTTPD setup, I do not have this > environment variable around. > > Any help would be greatly appreciated. > > Regards, > Nathaniel. > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > Nathaniel S. H. Brown http://nshb.net > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Ezra Zygmuntowicz
2005-Dec-31 20:02 UTC
[Rails] Unable to get the domain in my routes.rb to map custom route tables per domain
Nathaniel- I don''t think you are going to be able to do this in routes. I could be wrong but I dont think the request obje3ct is available to routes in the way that you want. I think its more likely that you could use a before filter in application.rb to do a redirect to the correct url based on the subdomain. But I could be misunderstanding what you are trying to do. Any more details of what you want to accomplish? Cheers- -Ezra On Dec 30, 2005, at 11:12 PM, Nathaniel S. H. Brown wrote:> I am still struggling with this. Anyone able to point me in the right > direction? > > -Nb > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > Nathaniel S. H. Brown http://nshb.net > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > >> -----Original Message----- >> From: rails-bounces@lists.rubyonrails.org >> [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of >> Nathaniel S. H. Brown >> Sent: December 30, 2005 5:01 AM >> To: rails@lists.rubyonrails.org >> Subject: [Rails] Unable to get the domain in my routes.rb to >> map custom route tables per domain >> >> I have tried all night trying to find a way to get the host >> name which is currently being used within my routes.rb file >> to do some case/when switching for specific domains such as this >> >> cgi = ActionController::CgiRequest.new(CGI.new) >> case cgi.domain(1) >> when /www/ >> map.connect '''', :controller => >> ''public'', :action => ''index'' >> when /internal/ >> map.connect '''', :controller => ''admin'', >> :action => ''index'' >> end >> >> And have had no success. The domain(1) is empty in this case. >> >> I have tried a similar approach in one of my views, using the >> @request.domain(1) and that works PERFECT. Yet I have found >> no way to access the same @request.domain(1) within my >> routes.rb as the @request object has not been loaded yet. >> >> One thought was to use the ENV[''HTTP_HOST''] variable, but >> with my current LIGHTTPD setup, I do not have this >> environment variable around. >> >> Any help would be greatly appreciated. >> >> Regards, >> Nathaniel. >> >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> Nathaniel S. H. Brown http://nshb.net >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra@yakima-herald.com 509-577-7732
Abdur-Rahman Advany
2005-Dec-31 20:14 UTC
[Rails] Unable to get the domain in my routes.rb to map custom route tables per domain
Hi, Please look at http://dev.rubyonrails.org/browser/plugins and then account_location <http://dev.rubyonrails.org/browser/plugins/account_location>, this is something your looking for I think... Ezra Zygmuntowicz wrote:> Nathaniel- > > I don''t think you are going to be able to do this in routes. I > could be wrong but I dont think the request obje3ct is available to > routes in the way that you want. I think its more likely that you > could use a before filter in application.rb to do a redirect to the > correct url based on the subdomain. But I could be misunderstanding > what you are trying to do. Any more details of what you want to > accomplish? > > Cheers- > -Ezra > > > On Dec 30, 2005, at 11:12 PM, Nathaniel S. H. Brown wrote: > >> I am still struggling with this. Anyone able to point me in the right >> direction? >> >> -Nb >> >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> Nathaniel S. H. Brown http://nshb.net >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> >> >>> -----Original Message----- >>> From: rails-bounces@lists.rubyonrails.org >>> [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of >>> Nathaniel S. H. Brown >>> Sent: December 30, 2005 5:01 AM >>> To: rails@lists.rubyonrails.org >>> Subject: [Rails] Unable to get the domain in my routes.rb to >>> map custom route tables per domain >>> >>> I have tried all night trying to find a way to get the host >>> name which is currently being used within my routes.rb file >>> to do some case/when switching for specific domains such as this >>> >>> cgi = ActionController::CgiRequest.new(CGI.new) >>> case cgi.domain(1) >>> when /www/ >>> map.connect '''', :controller => >>> ''public'', :action => ''index'' >>> when /internal/ >>> map.connect '''', :controller => ''admin'', >>> :action => ''index'' >>> end >>> >>> And have had no success. The domain(1) is empty in this case. >>> >>> I have tried a similar approach in one of my views, using the >>> @request.domain(1) and that works PERFECT. Yet I have found >>> no way to access the same @request.domain(1) within my >>> routes.rb as the @request object has not been loaded yet. >>> >>> One thought was to use the ENV[''HTTP_HOST''] variable, but >>> with my current LIGHTTPD setup, I do not have this >>> environment variable around. >>> >>> Any help would be greatly appreciated. >>> >>> Regards, >>> Nathaniel. >>> >>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >>> Nathaniel S. H. Brown http://nshb.net >>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > -Ezra Zygmuntowicz > WebMaster > Yakima Herald-Republic Newspaper > ezra@yakima-herald.com > 509-577-7732 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Nathaniel S. H. Brown
2006-Jan-01 01:22 UTC
[Rails] Unable to get the domain in my routes.rb to map customroute tables per domain
I have two domains setup on my rails app: example.COM and example.NET I want to be able to have a case in the routes, that if the URL in the location bar is example.NET to use this route: if hostname.match(/NET/) map.connect '''', :controller => ''special'', :action => ''case'' else map.connect '''', :controller => ''default'', :action => ''index'' End That way I can have a new root URL for example.NET, but use the same application. This should definitely be available as it is available through the @request.domain(1) within the view. Question is, if its not available in a default variable, how can I populate it manually? I checked out the CGI module from Ruby, and it mentions to use the ENV[''HTTP_HOST''] variable, but that is empty. So I am led to believe that Rails gets the active domain through some other mechanism, yet I am unable to find where this is done. Thanks. -Nb ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Nathaniel S. H. Brown http://nshb.net ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > Ezra Zygmuntowicz > Sent: December 31, 2005 12:03 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Unable to get the domain in my routes.rb > to map customroute tables per domain > > Nathaniel- > > I don''t think you are going to be able to do this in > routes. I could be wrong but I dont think the request obje3ct > is available to routes in the way that you want. I think its > more likely that you could use a before filter in > application.rb to do a redirect to the correct url based on > the subdomain. But I could be misunderstanding what you are > trying to do. Any more details of what you want to accomplish? > > Cheers- > -Ezra > > > On Dec 30, 2005, at 11:12 PM, Nathaniel S. H. Brown wrote: > > > I am still struggling with this. Anyone able to point me in > the right > > direction? > > > > -Nb > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > Nathaniel S. H. Brown http://nshb.net > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > > > > >> -----Original Message----- > >> From: rails-bounces@lists.rubyonrails.org > >> [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > Nathaniel > >> S. H. Brown > >> Sent: December 30, 2005 5:01 AM > >> To: rails@lists.rubyonrails.org > >> Subject: [Rails] Unable to get the domain in my routes.rb to map > >> custom route tables per domain > >> > >> I have tried all night trying to find a way to get the host name > >> which is currently being used within my routes.rb file to do some > >> case/when switching for specific domains such as this > >> > >> cgi = ActionController::CgiRequest.new(CGI.new) > >> case cgi.domain(1) > >> when /www/ > >> map.connect '''', :controller => > >> ''public'', :action => ''index'' > >> when /internal/ > >> map.connect '''', :controller => ''admin'', > :action => ''index'' > >> end > >> > >> And have had no success. The domain(1) is empty in this case. > >> > >> I have tried a similar approach in one of my views, using the > >> @request.domain(1) and that works PERFECT. Yet I have > found no way to > >> access the same @request.domain(1) within my routes.rb as the > >> @request object has not been loaded yet. > >> > >> One thought was to use the ENV[''HTTP_HOST''] variable, but with my > >> current LIGHTTPD setup, I do not have this environment variable > >> around. > >> > >> Any help would be greatly appreciated. > >> > >> Regards, > >> Nathaniel. > >> > >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > >> Nathaniel S. H. Brown http://nshb.net > >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails@lists.rubyonrails.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -Ezra Zygmuntowicz > WebMaster > Yakima Herald-Republic Newspaper > ezra@yakima-herald.com > 509-577-7732 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Nathaniel S. H. Brown
2006-Jan-01 01:25 UTC
[Rails] Unable to get the domain in my routes.rb to map customroute tables per domain
After looking at this, it appears this still doesn''t address the functionality that I want. I don''t want to redirect to a specific controller and action, I want to map it directly to the root URL within the route. So http://example.NET/ and http://example.ORG/ can use the same Rails install, yet have two totally different default routes. -Nb ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Nathaniel S. H. Brown http://nshb.net ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > Abdur-Rahman Advany > Sent: December 31, 2005 12:14 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Unable to get the domain in my routes.rb > to map customroute tables per domain > > Hi, > > Please look at http://dev.rubyonrails.org/browser/plugins and > then account_location > <http://dev.rubyonrails.org/browser/plugins/account_location>, > this is something your looking for I think... > > Ezra Zygmuntowicz wrote: > > Nathaniel- > > > > I don''t think you are going to be able to do this in routes. I > > could be wrong but I dont think the request obje3ct is available to > > routes in the way that you want. I think its more likely that you > > could use a before filter in application.rb to do a redirect to the > > correct url based on the subdomain. But I could be misunderstanding > > what you are trying to do. Any more details of what you want to > > accomplish? > > > > Cheers- > > -Ezra > > > > > > On Dec 30, 2005, at 11:12 PM, Nathaniel S. H. Brown wrote: > > > >> I am still struggling with this. Anyone able to point me > in the right > >> direction? > >> > >> -Nb > >> > >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > >> Nathaniel S. H. Brown http://nshb.net > >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > >> > >> > >>> -----Original Message----- > >>> From: rails-bounces@lists.rubyonrails.org > >>> [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > Nathaniel > >>> S. H. Brown > >>> Sent: December 30, 2005 5:01 AM > >>> To: rails@lists.rubyonrails.org > >>> Subject: [Rails] Unable to get the domain in my routes.rb to map > >>> custom route tables per domain > >>> > >>> I have tried all night trying to find a way to get the host name > >>> which is currently being used within my routes.rb file to do some > >>> case/when switching for specific domains such as this > >>> > >>> cgi = ActionController::CgiRequest.new(CGI.new) > >>> case cgi.domain(1) > >>> when /www/ > >>> map.connect '''', :controller => ''public'', :action => > >>> ''index'' > >>> when /internal/ > >>> map.connect '''', :controller => ''admin'', :action => > >>> ''index'' > >>> end > >>> > >>> And have had no success. The domain(1) is empty in this case. > >>> > >>> I have tried a similar approach in one of my views, using the > >>> @request.domain(1) and that works PERFECT. Yet I have > found no way > >>> to access the same @request.domain(1) within my routes.rb as the > >>> @request object has not been loaded yet. > >>> > >>> One thought was to use the ENV[''HTTP_HOST''] variable, but with my > >>> current LIGHTTPD setup, I do not have this environment variable > >>> around. > >>> > >>> Any help would be greatly appreciated. > >>> > >>> Regards, > >>> Nathaniel. > >>> > >>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > >>> Nathaniel S. H. Brown http://nshb.net > >>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > >>> > >>> _______________________________________________ > >>> Rails mailing list > >>> Rails@lists.rubyonrails.org > >>> http://lists.rubyonrails.org/mailman/listinfo/rails > >>> > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails@lists.rubyonrails.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > > > > -Ezra Zygmuntowicz > > WebMaster > > Yakima Herald-Republic Newspaper > > ezra@yakima-herald.com > > 509-577-7732 > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
D Andrew Reynhout
2006-Jan-13 22:37 UTC
routing by subdomain (was: Re: [Rails] Unable to get the domain in my routes.rb to map customroute tables per domain)
Re: problems with Rails routing by hostname, e.g.: http://www.verybigsite.com/ --> :controller => ''sites'', :action => ''list'' http://sanfrancisco.verybigsite.com/ --> :controller => ''sites'', :action => ''show'', :id => ''sanfrancisco'' http://boston.verybigsite.com/ --> :controller => ''sites'', :action => ''show'', :id => ''boston'' I really wanted to make this work in routing, not in URL rewriting, before_filters, etc. Here''s my understanding of the problem, and my simple but kludgy solution (suggestions welcomed!). (running current versions of lighttpd, fastcgi, and rails) First of all, lighttpd doesn''t seem to pass the useful bits of the HTTP request header in environment variables. Or perhaps FastCGI doesn''t forward them along to Rails. This might be a configuration problem. Anyway, Rails builds the route map at startup, and (in development mode ONLY) before each request. The CGI request is fully parsed before the route is followed, but since routes are not normally reloaded for each request, there would be no value in passing the request data for routing decision making...so it isn''t. So we have to do two things: - Store the request data somewhere persistent (or pass it to the routing code) - Reload the routes for each request Depending on your circumstances, this might cause an unacceptable performance hit. My routes are lightweight, so I decided to give it a spin. Very limited testing suggests that reloading routes is not a big deal at all. Here''s what I did... A couple of small changes to dispatcher.rb (my RubyGems install puts it in /usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib): --- dispatcher.rb-orig Fri Jan 13 16:37:42 2006 +++ dispatcher.rb Fri Jan 13 16:38:29 2006 @@ -34,6 +34,7 @@ def dispatch(cgi = nil, session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout) if cgi ||= new_cgi(output) request, response = ActionController::CgiRequest.new(cgi, session_options), ActionController::CgiResponse.new(cgi) + ENV[''REQUEST_HOST'']=request.host ## set env variable prepare_application ActionController::Routing::Routes.recognize!(request).process(request, response).out(output) end @@ -66,7 +67,7 @@ end def prepare_application - ActionController::Routing::Routes.reload if Dependencies.load? + ActionController::Routing::Routes.reload ## force reload prepare_breakpoint Controllers.const_load!(:ApplicationController, "application") unless Controllers.const_defined?(:ApplicationController) end And then in config/routes.rb: hostname=ENV[''REQUEST_HOST''] if hostname and hostname != ''bigsite.com'' and hostname != ''www.bigsite.com'' cityname = hostname.split(''.'').first map.connect '''', :controller => ''sites'', :action => ''show'', :id => cityname else map.connect '''', :controller => ''sites'', :action => ''list'' end ...it works. Better ideas welcomed! Andrew On Sat, Dec 31, 2005 at 05:22:04PM -0800, Nathaniel S. H. Brown wrote:> I have two domains setup on my rails app: > > example.COM and example.NET > > I want to be able to have a case in the routes, that if the URL in the > location bar is example.NET to use this route: > > if hostname.match(/NET/) > map.connect '''', :controller => ''special'', :action => ''case'' > else > map.connect '''', :controller => ''default'', :action => ''index'' > End > > That way I can have a new root URL for example.NET, but use the same > application. >