Forum: Ruby on Rails Displaying based on hostname/subdomain

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Joe N. (Guest)
on 2006-01-25 14:09
(Received via mailing list)
Ive tried multiple ideas to get this to work, but keep hitting a dead
end.  Has anyone done this before?

I want to display

Im using lighttpd.

Routes dont have access to the URL string and therefore I cant code in
a conditional.  lighttpd rewrite seems to really screw it all up.  Is
there a feasible solution?

Thank you,

Joe N.
Ben M. (Guest)
on 2006-01-25 18:27
(Received via mailing list)
Maybe I missed something previously in this thread, but can't you just

# handish virtual hosting
# map all domains of a top-level-domain to a single document-root
$HTTP["host"] =~ "(^|\.)example\.org$" {
   server.document-root = "/var/www/htdocs/"

Taken from here:


PS: anyone know what "handish" means?
Joe N. (Guest)
on 2006-01-25 20:25
(Received via mailing list)
> $HTTP["host"] =~ "(^|\.)example\.org$" {
>    server.document-root = "/var/www/htdocs/"
> }

I don't want to do anything with document root, since document root
will always be the same for the whole project no matter whats params
I'm passing in.

To further explain, I need to get to translate to (:controller "profile" :action "show"
:id "joe")

I don't want to redirect. Ive thought of using the render_component,
but that starts to become ugly.  Any other solutions out there?

Joe N.
Dylan S. (Guest)
on 2006-01-25 20:34
(Received via mailing list)
Joe, something like this maybe ?

evhost isn't very well documented, but this shows the available
variables at
Ben M. (Guest)
on 2006-01-25 21:56
(Received via mailing list)
Yeah, I see your point... configuring in lighty would map different
subdomains to
different apps. But you want it all to go to the same app, remapped to
an action based on
the subdomain.

I think your only option is to map everything at to your app
(in lighty) and then
write something on the rails side to reinterpret the url as needed.

The stock comment in application.rb says "Filters added to this
controller will be run for
all controllers in the application". So, sounds like you need to write a
filter that
checks the host for <username> and fowards internally based on
what it finds.

That or maybe it's possible to get access to the host name in
routes.rb... ???

I'm too green to throw out the code... any takers out there?

Bill K. (Guest)
on 2006-01-25 22:14
(Received via mailing list)
How about using something like the following in a before_filter in

    # Grab the subdomain and render appropriately
    @subdomain = request.subdomains.first
    params[:id] = @subdomain
    render :controller => 'profile', :action => 'show'

Does that make any sense?
Ezra Z. (Guest)
on 2006-01-25 22:14
(Received via mailing list)
Here is one way to solve you problem. Its slightly different from
what you wanted as far as mapping to the url you want but its another
way to use the subdomain as account key:

In application.rb
# The filters added to this controller will be run for all
controllers in the application.
# Likewise will all the methods added be available for all controllers.
require 'user_system'
require 'account_location'

class ApplicationController < ActionController::Base
   include UserSystem
   include AccountLocation

    before_filter :setup_environment, :except =>
[:new, :create, :signup]


    def setup_environment
	  if @account = Account.find_by_company_abbrev
           @session['account_id'] ||=
            redirect_to :controller => 'user',  :action => 'login'
in lib/account_location
module AccountLocation
   def self.included(controller)
(:account_domain, :account_host, :account_url)

     def default_account_subdomain
       @account.company_abbrev if @account && @account.respond_to?
(:company_abbrev) || "default"

     def account_url(account_subdomain = default_account_subdomain,
use_ssl = request.ssl?)
       (use_ssl ? "https://" : "http://") + account_host

     def account_host(account_subdomain = default_account_subdomain)
       account_host = ""
       account_host << account_subdomain + "."
       account_host << account_domain

     def account_domain
       account_domain = ""
       account_domain << request.subdomains[1..-1].join(".") + "." if
request.subdomains.size > 1
       account_domain << request.domain + request.port_string

	With this method I have an Account model that stores the username as
the subdomain key. this before filter gets run on each request and
takes the subdomain from the url and grabs the user that matches it.
Then it sets session[:account_id] to unless it is already
set. The rest of the methods in the AccountLocation module are just
for creating the correct links that always include the subdomain.

	So with this method, instead of redirecting to, the url just always stays
whenever they are logged in. Much simpler then what you want to do I
think ;-)


-Ezra Z.
Yakima Herald-Republic
This topic is locked and can not be replied to.