# Question for rails multidomain application design

**URL:** <https://www.ruby-forum.com/t/question-for-rails-multidomain-application-design/118855>\
**Category:** Rails\
**Created:** [October 31, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/question-for-rails-multidomain-application-design/118855 "2007-10-31T18:18:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gianluca\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d78d45/32.png) [@gianluca\_t](https://www.ruby-forum.com/u/gianluca_t)\
**Post date:** [October 31, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/question-for-rails-multidomain-application-design/118855/1 "2007-10-31T18:18:34Z")

</div>

Hi to all,

I would design an application for multidomain support and my question is  
what is the best practice to do this kind of application ?

Eg.:

I think to write my application handling the multidomain into routing  
map:

map.connect “:domain/:controller/:action”  
map.connect “:domain/:controller/:action/:id”

Assuming that I hanlde 3 domains for 3 different customers:

[www.first.com](http://www.first.com)  
[www.second.com](http://www.second.com)

> **[GapGun and Vectro Non-Contact Laser Measurement](https://www.third.com/)**
>
> Measure gap and flush using GapGun and Vectro (hand-held and automated) non-contact precision feature measurement tools by Third Dimension.

My customers requests could be:

[http://www.first.com/public/home](http://www.first.com/public/home)  
[http://www.second.com/public/home](http://www.second.com/public/home)

> **[GapGun and Vectro Non-Contact Laser Measurement](https://www.third.com/)**
>
> Measure gap and flush using GapGun and Vectro (hand-held and automated) non-contact precision feature measurement tools by Third Dimension.

routed to RoR application:

/www.first.com/public/home  
/www.second.com/public/home  
/www.third.com/public/home

RoR application controller could read the home data from model finding  
by domain param (es. @home = Home.find\_by\_domain(params[:domain])

I would like to know if it is possible (and how can I solve) this kind  
of routing (maybe using Apache internal rewrite ?).

Thanks in advance !

---

<div class="post-metadata">

**Author:** ![gianluca\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d78d45/32.png) [@gianluca\_t](https://www.ruby-forum.com/u/gianluca_t)\
**Post date:** [November 1, 2007, 1:33pm UTC](https://www.ruby-forum.com/t/question-for-rails-multidomain-application-design/118855/2 "2007-11-01T13:33:10Z")

</div>

Gianluca,  
I approached the same proble by using the domain as a unique :id and  
‘scoping’ the entire database based on the incoming request.host.  
Kathy

On Oct 31, 10:13 am, Gianluca T.

---

<div class="post-metadata">

**Author:** ![gianluca\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d78d45/32.png) [@gianluca\_t](https://www.ruby-forum.com/u/gianluca_t)\
**Post date:** [November 1, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/question-for-rails-multidomain-application-design/118855/3 "2007-11-01T19:03:43Z")

</div>

Why not something like this instead?

class ApplicationController \< ActionController::Base  
before\_filter :setup\_application\_by\_domain  
protected  
def setup\_application\_by\_domain  
@domain = Domain.find\_by\_name(request.domain)  
# continue with your setup  
end  
end

The mapping idea, can be a little messy, specialy if you forgot to map  
an action, i think it’s better to get the domain information from the  
request object, and setup an instance of some model with all the  
information you need.

On Oct 31, 10:13 am, Gianluca T.

---

<div class="post-metadata">

**Author:** ![gianluca\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d78d45/32.png) [@gianluca\_t](https://www.ruby-forum.com/u/gianluca_t)\
**Post date:** [November 1, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/question-for-rails-multidomain-application-design/118855/4 "2007-11-01T20:02:49Z")

</div>

On Oct 31, 2007, at 9:13 AM, Gianluca T. wrote:

> I would design an application for multidomain support and my  
> question is  
> what is the best practice to do this kind of application ?
> 
> My customers requests could be:
> 
> [http://www.first.com/public/home](http://www.first.com/public/home)  
> [http://www.second.com/public/home](http://www.second.com/public/home)  
> [GapGun and Vectro Non-Contact Laser Measurement](http://www.third.com/public/home)

I haven’t done this in Rails yet, but I’ve written several multi-  
domain (multi-subdomain) apps in another language, and IMO it is best  
to initialize an object as one of your first steps (so withing  
application.rb) which contains the baseline info the app needs:  
domain name, layout preferences, options features if applicable, etc.

So, take all those configuration details you might otherwise have in  
code, and push them into a db. Then the first task you do is querythe  
db for the config details for X domain or subdomain. From that point  
on your app is really like any single-domain app except that you use  
the domain name to identify a specific database if you use a db per  
domain, or it defines how you filter all db queries if you use one db  
for all domains.

It really is easier than it usually first sounds like.

From what I can tell, even in Rails, there’s no need to map domain  
to a route because the domain won’t change the actual actions you’re  
calling. You want the actions themselves to adjust their behavior  
based on domain which is no different than having them change based  
on a specific logged in user. So, just like you would not change  
routes based on users, you don’t need to do it based on domains. Make  
sense?

– gw

---

<div class="post-metadata">

**Author:** ![gianluca\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d78d45/32.png) [@gianluca\_t](https://www.ruby-forum.com/u/gianluca_t)\
**Post date:** [November 6, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/question-for-rails-multidomain-application-design/118855/5 "2007-11-06T16:06:57Z")

</div>

Gianluca,  
Once you’ve learned to ‘scope’ the @domain table based on the current  
request.domain the rest is downhill.  
If I had a table called ‘content’ I’d use the before filter to find  
the domain and then gather the collection like;  
@content = @domain.contents.find(:all) and so on for any table in your  
application.  
Kathleen

On Nov 6, 3:54 am, Gianluca T.

---

<div class="post-metadata">

**Author:** ![gianluca\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d78d45/32.png) [@gianluca\_t](https://www.ruby-forum.com/u/gianluca_t)\
**Post date:** [November 6, 2007, 11:01am UTC](https://www.ruby-forum.com/t/question-for-rails-multidomain-application-design/118855/6 "2007-11-06T11:01:10Z")

</div>

Thanks Kathy, Pablo and Greg, u r right.

Your suggestions was very good and drive me into right direction,

I solved the problem only setting 3 virtual hosts pointing to the same  
rails app and simply setting the base application controller filter (no  
routing map at all !):

@domain = Domain.find\_by\_name(request.domain)

Last question:

How did u solve the different public folder for each domain ?

In my application static contents must be separated for each different  
domain [www.first.com](http://www.first.com) / [www.second.com](http://www.second.com) / [www.third.com](http://www.third.com) so I think I must  
have 3 different public folder, one for each different domain ?

How can handle this into rails application ? (one possible solution is  
to handle a parameter for images and stylesheets “:path” but I think  
there is a better solution because in this case I have to use tha  
“:path” parameter into each view…).

Thanks in advance…

## – Gianluca T. TreNetMediaMaster S.r.l. The Internet Technology Company Telefono +39(049)776196 Fax +39(049)8087806 Visit us @: [http://www.trenet.it](http://www.trenet.it) - [http://www.mediamaster.it](http://www.mediamaster.it) e-mail me @: removed\_email\_address@domain.invalid

Le informazioni trasmesse sono destinate esclusivamente alla persona o  
alla società in indirizzo e sono da intendersi confidenziali e riservate.  
Ogni trasmissione, inoltro, diffusione o altro uso di queste  
informazioni a persone o società differenti dal destinatario é proibita.  
Se ricevete questa comunicazione per errore, contattate il mittente e  
cancellate le informazioni da ogni computer.

---

<div class="post-metadata">

**Author:** ![gianluca\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d78d45/32.png) [@gianluca\_t](https://www.ruby-forum.com/u/gianluca_t)\
**Post date:** [November 6, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/question-for-rails-multidomain-application-design/118855/7 "2007-11-06T16:47:55Z")

</div>

Hi Kathleen,

thank you very much for your reply !

Thanks to your suggestion I solved the problem for accessing “dynamic”  
(database) contents.

The problem now is how to handle “static” contents for each different  
domain (eg. public/images folder on rails app is unique for all the  
domains and my goal is to handle static contents like  
images/javascripts/flash in different folders (one folder for each  
domain)).

One possible solution is to handle a parameter for images and  
stylesheets eg. @domain.path but I think there is a better solution  
because in this case I must set the @domain.path parameter into each  
view…

eg. ![]()

I would like to know if is there a better (dry) solution…

Thanks in advance.
