Routes dependent on subdomain

Hi,

I'm having little problem getting my idea to work, would be very
thankful if you could help me.

I have one Ruby on Rails application sitting on server, and multiple
subdomains, pointing to the same server/app. For example:

mainserver.com
user1.mainserver.com
user2.mainserver.com

I want to have exactly the same app and exactly the same routes on
all, except that I want to map root of mainserver.com to different
controller than other. For example:

mainserver.com => MainController
user1.mainserver.com => UserController
user2.mainserver.com => UserController

Any ideas how can I do it using Rails routes? Or possibly URL rewrite
in Apache proxy?

I am also considering different approach - I will have 2 applications
running, and share part of code (models, some base controllers), and
map main web page to different instance than all other subdomains.

Thanks and best,
H.