Routes + Subdomains

3 views
Skip to first unread message

Bartłomiej Romański

unread,
Sep 4, 2006, 5:18:17 PM9/4/06
to pylons-discuss
Hi,

I'd like to rewrite my app using pylons but I've noticed the problem at
very beginning.

I offer some stuff for users and use subdomains to do so. So
www.mysite.com is main page of the site, and user.mysite.com is a user
specyfic page (eg. blog or something). And my questions is how to map
this using routes?

One possible solution is to rewrite URLs at web server level from
xxx.mysite.com/ to mysite.com/xxx/ and map it with routes. It's ok, but
I've got 2 questions.

First, how can I write a middleware or something to make this rewrite
at pylons level?

Second, what to do not to break helpers like url_for() and link_to()? I
could just write a new version of url_for and substitute it, but I
don't know how to get into python mechanizm...

Is there such system in pylons like in ruby on rails so I can just
write plugin which will do URL rewrite before mapping and which will
add something like decorator to url_for?

I can't find any documentation on how to this... Thanks for any help.

rikl...@gmail.com

unread,
Sep 4, 2006, 8:35:49 PM9/4/06
to pylons-discuss
in apache you could use wildcard and some friends (I'm not an expert at
this) and then all subdomains would point in the same spot (main
folder, where www.mysite.pl is directed) and then in the code you would
just extract username from the in-url domain :) django uses simillar
approach where you can assign IDs for "domains" and then in the code
take action basing on the ID.

Ben Bangert

unread,
Sep 4, 2006, 9:08:34 PM9/4/06
to pylons-discuss
Routes currently doesn't provide a very easy way to do this, so I've
added one. If you update your routes to the latest:
easy_install -U Routes==dev

You can now use an additional map option in yourproj/lib/routing.py,
map.sub_domains = True

And either require a sub domain to be present:
map.connect('', controller='user', action='home',
conditions=dict(sub_domains=True))

Or require a range of specific sub-domains:
map.connect('', controller='user', action='home',
conditions=dict(sub_domains=['fred','george']))

If you have multiple sub-domains that are equivilant to your hostname
(www.host.com == host.com), you can ignore those with:
map.sub_domains_ignore =['www']

url_for will now also handle the sub_domain keyword, ie:
url_for(action='list', sub_domain='george')

This will generate a URL with the sub-domain of 'george' based off the
current domain information. To determine the current domain name, a
regexp of: [^\.\/]+?\.[^\.\/]+ is used. Which will map
anything.anything, if you have a longer base (ie, domain.com.pl), you
can override the match:
map.domain_match = 'new_match_here'

Let me know how it works out for you.

HTH,
Ben

Ben Bangert

unread,
Sep 4, 2006, 9:12:12 PM9/4/06
to pylons-discuss
I should also note, that when you use map.sub_domains = True, your
actions can always ask for a sub_domain arg and act on it if desired,
ie:

def view(self, id, sub_domain):
if sub_domain:
pass # do something with it

Cheers,
Ben

Bartłomiej Romański

unread,
Sep 5, 2006, 2:51:25 PM9/5/06
to pylons-...@googlegroups.com
Great, looks cool. I'll try it soon. Thanks!
Reply all
Reply to author
Forward
0 new messages