views:

33

answers:

1

I am trying to have a custom URL which looks like this:

example.com/site/yahoo.com

which would hit this script like this=

example.com/details?domain=yahoo.com

can this be done using app.yaml?

the basic idea is to call "details" with the input "yahoo.com"

+2  A: 

You can't really rewrite the URLs per se, but you can use regular expression groups to perform a similar kind of thing.

In your app.yaml file, try something like:

handlers:
- url: /site/(.+)
  script: site.py

And in your site.py:

SiteHandler(webapp.RequestHandler):
    def get(self, site):
        # the site parameter will be what was passed in the URL!
        pass

def main():
    application = webapp.WSGIApplication([('/site/(.+)', SiteHandler)], debug=True)
    util.run_wsgi_app(application)

What happens is, whatever you have after /site/ in the request URL will be passed to SiteHandler's get() method in the site parameter. From there you can do whatever it is you wanted to do at /details?domain=yahoo.com, or simply redirect to that URL.

Jason Hall
Perfect answer! thanks a ton for being so quick in response.
demos
Note that the regex in app.yaml can be anything that matches, and it doesn't need the capturing parens. Commonly, you'd just use .* to send everything to your script, and use more specific regexes (with the capturing parens) in your WSGI app.
Nick Johnson