Using dynamic subdomains with pyramid

186 views
Skip to first unread message

Alejandro Jatib

unread,
Jul 12, 2012, 11:51:02 PM7/12/12
to pylons-...@googlegroups.com
Hey folks,
I want to use dynamic subdomains as follows:

When someone enters in these directions should be redirected to:

I tried several examples I found and I end up doing this:

in __init__.py:

    def pregen(request, elements, kw):
        #kw['_app_url'] = 'http://www.domainapp.com/' + request.host.split('.')[0]
        return elements, kw

    def req_sub(info, request):
        return request.host.split('.')[0] != 'www'
    
    config.add_route('sub_only', '/{id:.*?}',
                 custom_predicates=(req_sub,),
                 #pregenerator=pregen
                 )

in views.py:

@view_config(route_name='sub_only')
def Subdomain(request):
        # redirect the user to "http://sub.example.com", regardless of whether
        # request.host is "example.com" or "sub.example.com"
        return HTTPFound(location='http://www.domainapp.com/' + request.host.split('.')[0])


Any suggestions to make it better?

Thanks

Jonathan Vanasco

unread,
Jul 13, 2012, 11:00:21 AM7/13/12
to pylons-discuss
I just do the URL rewriting like this on my proxy server or load
balancers.

It's way easier to just have nginx , varnish, etc handle this.

Alejandro Jatib

unread,
Jul 13, 2012, 3:34:28 PM7/13/12
to pylons-...@googlegroups.com
I forget to say that i was using pyramid_appengine and hosting my app in appengine.

Michael Merickel

unread,
Jul 13, 2012, 3:38:00 PM7/13/12
to pylons-...@googlegroups.com
If you can't do this on the web server as Jonathan suggested then I would do it as a custom middleware that wraps your Pyramid app. It could check the request's host and mutate the script_name/path_info as necessary before it gets to Pyramid.

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/BxECKuWWd_4J.

To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.

Reply all
Reply to author
Forward
0 new messages