# Displaying based on hostname/subdomain

**URL:** <https://www.ruby-forum.com/t/displaying-based-on-hostname-subdomain/97179>\
**Category:** Rails\
**Created:** [January 25, 2006, 1:09pm UTC](https://www.ruby-forum.com/t/displaying-based-on-hostname-subdomain/97179 "2006-01-25T13:09:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joenova](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joenova](https://www.ruby-forum.com/u/joenova)\
**Post date:** [January 25, 2006, 1:09pm UTC](https://www.ruby-forum.com/t/displaying-based-on-hostname-subdomain/97179/1 "2006-01-25T13:09:58Z")

</div>

Ive tried multiple ideas to get this to work, but keep hitting a dead  
end. Has anyone done this before?

I want [joe.domain.com](http://joe.domain.com) to display [www.domain.com/profile/joe](http://www.domain.com/profile/joe)

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.

---

<div class="post-metadata">

**Author:** ![joenova](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joenova](https://www.ruby-forum.com/u/joenova)\
**Post date:** [January 25, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/displaying-based-on-hostname-subdomain/97179/2 "2006-01-25T17:27:09Z")

</div>

Maybe I missed something previously in this thread, but can’t you just  
do:

# 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/example.org/pages/”  
}

Taken from here:

[http://www.lighttpd.net/documentation/configuration.html](http://www.lighttpd.net/documentation/configuration.html)

b

PS: anyone know what “handish” means?

---

<div class="post-metadata">

**Author:** ![joenova](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joenova](https://www.ruby-forum.com/u/joenova)\
**Post date:** [January 25, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/displaying-based-on-hostname-subdomain/97179/3 "2006-01-25T19:25:36Z")

</div>

> $HTTP[“host”] =~ “(^|.)example.org$” {  
> server.document-root = “/var/www/htdocs/example.org/pages/”  
> }

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 [joe.test.com](http://joe.test.com) to translate to  
[www.test.com/profile/show/joe](http://www.test.com/profile/show/joe) (: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.

---

<div class="post-metadata">

**Author:** ![joenova](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joenova](https://www.ruby-forum.com/u/joenova)\
**Post date:** [January 25, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/displaying-based-on-hostname-subdomain/97179/4 "2006-01-25T19:34:41Z")

</div>

Joe, something like this maybe ?  
[http://textsnippets.com/posts/show/5](http://textsnippets.com/posts/show/5)

evhost isn’t very well documented, but this shows the available  
variables at  
least.

---

<div class="post-metadata">

**Author:** ![joenova](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joenova](https://www.ruby-forum.com/u/joenova)\
**Post date:** [January 25, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/displaying-based-on-hostname-subdomain/97179/5 "2006-01-25T20:56:53Z")

</div>

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 [test.com](http://test.com) 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 .test.com 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?

b

---

<div class="post-metadata">

**Author:** ![joenova](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joenova](https://www.ruby-forum.com/u/joenova)\
**Post date:** [January 25, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/displaying-based-on-hostname-subdomain/97179/6 "2006-01-25T21:14:17Z")

</div>

How about using something like the following in a before\_filter in  
application.rb?

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

```

Does that make any sense?  
-Bill

---

<div class="post-metadata">

**Author:** ![joenova](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@joenova](https://www.ruby-forum.com/u/joenova)\
**Post date:** [January 25, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/displaying-based-on-hostname-subdomain/97179/7 "2006-01-25T21:14:17Z")

</div>

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]

```
protected

def setup_environment
  if @account = Account.find_by_company_abbrev

```

## (request.subdomains.first) @session[‘account\_id’] ||= @account.id else redirect\_to :controller =\> ‘user’, :action =\> ‘login’ end end end

## in lib/account\_location

module AccountLocation  
def self.included(controller)  
controller.helper\_method  
(:account\_domain, :account\_host, :account\_url)  
end

protected  
def default\_account\_subdomain  
@account.company\_abbrev if @account && @account.respond\_to?  
(:company\_abbrev) || “default”  
end

```
 def account_url(account_subdomain = default_account_subdomain,

```

use\_ssl = request.ssl?)  
(use\_ssl ? “https://” : “http://”) + account\_host  
(account\_subdomain)  
end

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

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

```

request.subdomains.size \> 1  
account\_domain \<\< request.domain + request.port\_string  
end  
end

```
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 @account.id 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 joe.example.com to

```

[example.com/user/joe](http://example.com/user/joe), the url just always stays [joe.example.com](http://joe.example.com)  
whenever they are logged in. Much simpler then what you want to do I  
think 😉

Cheers-

-Ezra Z.  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
removed\_email\_address@domain.invalid
