hoodwink.d enhanced


Dynamic VHosts One-liner for Rails #

by why in bits

So, how do Tada and Basecamp do dynamic virtual hosts to give each user their own subdomain? Drop this in ApplicationController:

 before_filter { |c| c.account =
   Account.find_by_subdomain(c.request.subdomains.first) }

Bravo. Requires a ServerName *.tadalist.com in the Apache VHost configuration and an accounts table with a subdomain field. (seen among #rubyonrails quotes.)

Update: If you’re struggling to get this to work, mpet45 has more. Configuring DNS and all that.

said on 31 Jan 2005 at 16:59


said on 31 Jan 2005 at 19:54

very nice.

said on 01 Feb 2005 at 08:19

The c variable refers to the instance of the controller that is passed to the filter when its triggered. This instance holds a reference to the request object, which again has an array of all the subdomains of which we need the first (so that we get “37signals” out of 37signals.clientsection.com).

Then we have the Account model that has a subdomain field, which in the case of 37s would have “37signals”. We search for the account matching this subdomain with the dynamic finder and assign it to the instance variable “account” on the controller instance we got in. Be sure to have something like “attr_writer :account” so that variable can be assigned.

Easy as pie.

said on 07 Feb 2005 at 12:30

In BIND (and most other DNS ), you can set up a catch-all for subdomains.

 *     IN CNAME @
said on 29 Mar 2006 at 08:12

very nice site!

Comments are closed for this entry.