defining sub-domain based on a function in routes.

2019-04-12 22:13发布

问题:

I have this page:

http://mysite.org/myapp/pages/blog?name=user9

and I need to have this:

user9.mysite.org

What should i write in my routes.py?

回答1:

Ok, so i think misinterpreted this a little. You need user9.mysite.org to be served from the web2py app. One way, if you have your site hosted at mysite.org, is to pass all requests (regardless of subdomain) to the web2py application (you'll need an A record like *.mysite.org with your DNS provider: http://kb.mediatemple.net/questions/791/DNS+Explained#/A_Record )

Then, you can use routes

Something like:

routes_in = (
  ('http://(?P<user>.*).mysite.org/(?P<any>.*)',
  '/app/pages/blog/\g<any>?name=\g<user>'),
) 

The <any> will save any args you may need. This should map a request from user9.mysite.org to mysite.org/app/pages/blog/<args>?name=user9

You might have to play with it a little to get it working. The key is to make sure that a request to any subdomain of mysite.org be given directly to the app. Meaning if you go to www.mysite.org, mysite.org, somerandomfakesubdomain.mysite.org, you'll always get to the same place as mysite.org. You'll probably want to put some logic in your blog function to ensure that the subdomain string (e.g. user9) represents a valid user.