# Sub-domain handling?

**URL:** <https://www.ruby-forum.com/t/sub-domain-handling/142069>\
**Category:** Rails\
**Created:** [July 3, 2008, 5:43am UTC](https://www.ruby-forum.com/t/sub-domain-handling/142069 "2008-07-03T05:43:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajmohan\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@Rajmohan\_B](https://www.ruby-forum.com/u/Rajmohan_B)\
**Post date:** [July 3, 2008, 5:43am UTC](https://www.ruby-forum.com/t/sub-domain-handling/142069/1 "2008-07-03T05:43:21Z")

</div>

All,

I am not sure if this is sub-domain handling, but am interested on how  
this  
can be handled in rails.

How can an app handle the traffic to, say - [http://part1.mydomain.com](http://part1.mydomain.com).  
The  
other normal traffic I guess will be handled by the main controller (of  
[www.mydomain.com](http://www.mydomain.com)). Any pointers to any tutorial/books appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![Rajmohan\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7993a0/32.png) [@Rajmohan\_B](https://www.ruby-forum.com/u/Rajmohan_B)\
**Post date:** [July 3, 2008, 6:32am UTC](https://www.ruby-forum.com/t/sub-domain-handling/142069/2 "2008-07-03T06:32:02Z")

</div>

On Thu, Jul 3, 2008 at 5:42 AM, Rajmohan B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am not sure if this is sub-domain handling, but am interested on how this  
> can be handled in rails.
> 
> How can an app handle the traffic to, say - [http://part1.mydomain.com](http://part1.mydomain.com). The  
> other normal traffic I guess will be handled by the main controller (of  
> [www.mydomain.com](http://www.mydomain.com)). Any pointers to any tutorial/books appreciated.

The basic steps are:

1. Use the account\_location plugin.

2. Make “www” a reserved subdomain.

3. Put these filters in ApplicationController:

# We need this redirection not only for users, I founded that some

bots

# request public pages to [myapplication.com](http://myapplication.com) directly. We need to

respond

# with the appropiate redirection so they don’t index the error page.

# They would because the error page is a successful HTTP response.

def ensure\_subdomain  
if account\_subdomain.blank?  
redirect\_to\_url  
“[http://www](http://www).#{account\_domain}#{request.request\_uri}”  
end  
end

def find\_account  
@current\_account = Account.find\_by\_short\_name(account\_subdomain)  
if !@current\_account  
logger.info(“there’s no account with short name  
#{account\_subdomain}, redirecting to the home”)  
redirect\_to\_url “[http://www](http://www).#{account\_domain}”  
end  
end

1. Be very careful, scope everything by @current\_account if necessary.  
For example modify User.authenticate to receive an account:

def self.authenticate(account, email, password)  
u = account.users.find\_by\_email(email) # user needed to get the salt  
u && u.authenticated?(password) ? u : nil  
end

and in general DO NOT use find over classes as in

Project.find(…) # WARNING, APPLICATION-WIDE

but instead

```
@current_account.projects.find(...) # GOOD

```

That’s a guideline and exceptions depend on the application, for example

@current\_user.projects.find(…)

may be already fine because you’ve validated that @current\_user  
belongs to @current\_account somehow etc.
