# Many domain - one rails application

**URL:** <https://www.ruby-forum.com/t/many-domain-one-rails-application/123458>\
**Category:** Rails\
**Created:** [December 17, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/many-domain-one-rails-application/123458 "2007-12-17T23:42:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kilo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@kilo](https://www.ruby-forum.com/u/kilo)\
**Post date:** [December 17, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/many-domain-one-rails-application/123458/1 "2007-12-17T23:42:52Z")

</div>

If I have many domains and one rails application, how can I redirect to  
conrollers by domain?

Ex. “[domain.com](http://domain.com)” go to controller “/com”, “[domain.net](http://domain.net)” go to controller  
“/net” and “[other.org](http://other.org)” go to “/other”

---

<div class="post-metadata">

**Author:** ![kilo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@kilo](https://www.ruby-forum.com/u/kilo)\
**Post date:** [December 18, 2007, 11:29am UTC](https://www.ruby-forum.com/t/many-domain-one-rails-application/123458/2 "2007-12-18T11:29:58Z")

</div>

On Dec 17, 2007, at 2:42 PM, Ki Lo wrote:

> If I have many domains and one rails application, how can I  
> redirect to  
> conrollers by domain?
> 
> Ex. “[domain.com](http://domain.com)” go to controller “/com”, “[domain.net](http://domain.net)” go to  
> controller  
> “/net” and “[other.org](http://other.org)” go to “/other”

You can extract the domain type from the request object (use  
request.domain and split on the period to get the last element)

I probably wouldn’t write different controllers if the differences  
are minor. If there is the need for multiple controllers, there’s  
probably still one controller you initially interface to which can  
change what it does based on the domain type (that is you pass the  
domain type to the controller and let it make conditional decisions  
– which might be factory pattern to launch a controller you describe  
above).

You might find some useful thoughts here:  
[http://www.railsdev.ws/blog/10/using-subdomains-in-rails-apps/](http://www.railsdev.ws/blog/10/using-subdomains-in-rails-apps/)

–  
def gw  
acts\_as\_n00b  
writes\_at(www.railsdev.ws)  
end

---

<div class="post-metadata">

**Author:** ![kilo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@kilo](https://www.ruby-forum.com/u/kilo)\
**Post date:** [December 18, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/many-domain-one-rails-application/123458/3 "2007-12-18T15:45:06Z")

</div>

On 18 Dec 2007, at 11:29, Greg W. wrote:

> request.domain and split on the period to get the last element)  
> You might find some useful thoughts here:  
> [WEBSITE.WS - Your Internet Address For Life™](http://www.railsdev.ws/blog/10/using-subdomains-in-rails-apps/)

The account\_location plugin provides nice helpers for these kinds of  
situations. However, a more appropriate solution in this case would  
be to use apache’s rewrite functions to rewrite the request to point  
to the correct controller. Saves you some conditioning and all the  
problems that may arise from it in your rails app.

Best regards

Peter De Berdt
