Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
get subdomain in localhost
4 Posts
get subdomain in localhost

Hi, I was wondering how to get subdomain name in my localhost. I'm on ubuntu and set domainnames in /etc/hosts like this : localhost : tokyo : sub.tokyo

when i was using puts request.domain it was showing "tokyo" when hit URL with http://tokyo:3002 and "sub.tokyo" when hit URL with http://sub.tokyo:3002

then, how i can i differ subdomains in sub.tokyo i tried using request.subdomains.first, but showing nil in localhost

Finaly i was looking for these when used puts request.domain.inspect ===> "localhost" or "tokyo" puts request.subdomains.first ===> "sub"

any help??

thanks, sri..

The @subdomains@ method isn't particularly smart, all it's doing is splitting on @.@ and returning all but the last two parts. So to get it to work in development then you will need to have the same number of parts to your domain as you will in production. So if you'd have @sub.foobar.com@ in production then you will need to have three part domain names in development too, so something like @sub.tokyo.dev@ mapped to your @ address.

"Take a look at the doco":http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=M000359&name=subdomains for more information for if you have a country code in your domain, you'll need to pass in an integer to the @subdomains@ method.

Hi king, My Production code looks like this !!

In routes.rb map.connect '', :controller => "home", :action => "home", :conditions => {:domain => "tokyo.com"}

and my environment.rb will look similar to this : I used :domain instead of :hostname http://pastie.org/321215

i registered sub.tokyo.com as subdomain under tokyo.com so when we hit http://sub.tokyo.com can we get the subdomain name so that i can differentiate my CSS and templates based on these two names if request.domain == "tokyo.com" if request.subdomains.first == "sub"

return true

else return false end else return false end

thanks, sri..

Yes, you can test the subdomain with @request.subdomains.first == "sub"@ - what I was saying was that in order to see this in your development environment as well you will need to have a three-part domain in your @/etc/hosts@ so instead of what you have with @ tokyo sub.tokyo@ you need to map @ to a three-part domain like: @ sub.tokyo.dev@ so that it has the same format to what you will have in production with @sub.tokyo.com@

Then the @request.subdomains.first@ will work both in production and development.

4 Posts
Login to add your message