# Routing to the main www subdomain for specific controllers

**URL:** <https://www.ruby-forum.com/t/routing-to-the-main-www-subdomain-for-specific-controllers/91132>\
**Category:** Rails\
**Created:** [April 4, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/routing-to-the-main-www-subdomain-for-specific-controllers/91132 "2007-04-04T23:52:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 4, 2007, 11:52pm UTC](https://www.ruby-forum.com/t/routing-to-the-main-www-subdomain-for-specific-controllers/91132/1 "2007-04-04T23:52:49Z")

</div>

Hi everyone!  
I’m using subdomains as user identifiers ([username.localhost.com](http://username.localhost.com) maps to  
the profile of that user). However, that complicates things as I do not  
want all controllers to work with those subdomains.

Let’s say I have a user controller with a signup action. I do not want  
people to be able to go to [anything.localhost.com/user/signup](http://anything.localhost.com/user/signup), and if  
they do I want them to be automatically redirected to  
[www.localhost.com/user/signup](http://www.localhost.com/user/signup). There would probably be lots of other  
controllers/actions I would like that to happen with.

On the other hand, the blog controller index should only displayed when  
accesses from a subdomain ([username.localhost.com/blog](http://username.localhost.com/blog)) but not from  
[www.localhost.com/blog](http://www.localhost.com/blog) (that would redirect back to the main site).

Question is how can i do this elegantly? Is there a way for me to  
automatically redirect to [www.localhost.com](http://www.localhost.com) in certain actions or do I  
have to check in the begining of each action if the subdomain is  
different the www and then redirect?

Any help would be much appreacited!  
Ehud

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 5, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/routing-to-the-main-www-subdomain-for-specific-controllers/91132/2 "2007-04-05T12:18:03Z")

</div>

Check out [http://agilewebdevelopment.com/plugins/request\_routing](http://agilewebdevelopment.com/plugins/request_routing)

On 04 Apr 2007, at 23:52, Ehud R. wrote:

> controllers/actions I would like that to happen with.
> 
> Any help would be much appreacited!

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 5, 2007, 1:05pm UTC](https://www.ruby-forum.com/t/routing-to-the-main-www-subdomain-for-specific-controllers/91132/3 "2007-04-05T13:05:40Z")

</div>

Peter De Berdt wrote:

> Check out [http://agilewebdevelopment.com/plugins/request\_routing](http://agilewebdevelopment.com/plugins/request_routing)
> 
> On 04 Apr 2007, at 23:52, Ehud R. wrote:
> 
> > controllers/actions I would like that to happen with.
> > 
> > Any help would be much appreacited!
> 
> Best regards
> 
> Peter De Berdt

Hi Peter,  
I’m already using that plugin to allow routing based on subdomains.  
The problem is with redirection, not really routing (I think…).

What i’m looking for is a way to redirect a call from  
[subdomian.localhost.com/controller/action](http://subdomian.localhost.com/controller/action) to  
[www.localhost.com/controller/action](http://www.localhost.com/controller/action) - and do it in the simplest way  
possible.

Ehud

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 5, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/routing-to-the-main-www-subdomain-for-specific-controllers/91132/4 "2007-04-05T13:23:14Z")

</div>

On Apr 5, 2007, at 1:05 PM, Ehud R. wrote:

> > 
> 
> [subdomian.localhost.com/controller/action](http://subdomian.localhost.com/controller/action) to  
> [www.localhost.com/controller/action](http://www.localhost.com/controller/action) - and do it in the simplest way

I have a high-priority filter in ApplicationController like this:

# Prevents account sites from accessing to the public side. This

filter

# assumes the root page is not under public, which is to be

expected, and

# redirects there if needed.

def check\_access\_to\_public  
if controller\_name == ‘public’ && account\_subdomain != ‘www’  
logger.info(“attempt to access to a public action from an  
account site”)  
redirect\_to\_url account\_url(account\_subdomain)  
return false  
end  
end

It does not do what you want, but that filter depicts a possible  
approach.

If your controllers do not mix public and private actions that filter  
may suffice. A set of controller names would be used instead of the  
hard-coded ‘public’ (my app has all the public stuff served by a  
single controller). If there are too many as you suggested in a  
previous mail, then you could have abstract controllers  
AbstractPublicController, AbstractPrivateController between  
ApplicationController and the rest, and test is\_a?  
(AbstractPublicController).

The redirection would be different as well, but you see the idea.

– fxn
