Google AppEngine: URL Rewriting

Back in February I showed you how to create a nice looking, dynamic home page on Google AppEngine. This implementation had major flaw – namely ugly URL’s. I have set it up so that the name of the page to be loaded was passed in as a variable in a GET request. So the URL’s looked a bit like this:

http://yourdomain.com/p=pagename

This is obviously less than desirable and looks a bit unprofessional. My whole idea was to make a professional, spiffy looking website that you could put on a business card or give out to prospective employers. The page was supposed to underline your competence, and professionalism. Ideally, you would want to have nice looking and easy to remember URL’s like this:

http://yourdomain.com/pagename

No question marks, equal signs, dots or file extensions. Just the domain name, and a keyword. In most cases you would achieve this effect by using URL Rewrite feature of Apache. All it takes to set something like this up is 1 or two lines that need to be added to your .htaccess file.

Unfortunately, Google AppEngine does not use Apache and uploading .htaccess file won’t do you any good. To achieve the effect I described above you need to hack the code in your controller class.

Here is the modified version of the main.py file from the previous article:

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

class MainPage(webapp.RequestHandler):
   def get(self, p):
      if p:
         page = p + ".html"
      else:
         p = "main"
         page = p + ".html"

      if not os.path.exists(page):
         page = "404.html"

      template_values = {
            "page" : page,
            p: "first", 
      };

      path = os.path.join(os.path.dirname(__file__), 'index.html')
      self.response.out.write(template.render(path, template_values))

application = webapp.WSGIApplication([(r'/(.*)', MainPage)],debug=True)

def main():
   run_wsgi_app(application)

if __name__ == "__main__":
   main()

I will save you doing a mental diff between the two files, and simply tell you what changed. You simply need to modify two lines. First, you need to change the WSGIApplication invocation to include this pattern matching code:

application = webapp.WSGIApplication([(r'/(.*)', MainPage)],debug=True)

Next, you need to change the method definition in your handler to include a variable like this:

def get(self, p):

You will also need to get rid of the line which checks the p variable in the GET request. After you add this code, everything found after the / in your URL will be passed to your handler as the value of the argument you just defined.

It is very simple, very elegant and allows you to have very clean looking and easy to remember urls.

This entry was posted in Uncategorized. Bookmark the permalink.



10 Responses to Google AppEngine: URL Rewriting

  1. Useful hack. I hate needlessly ugly URLs. When possible, a dynamic site should look like a simple file hierarchy without any GET parameters.

    Imagine if the URL to this post was the following!

    http://www.terminally-incoherent.com/wordpress/blog.php?year=2009&mont h=5&day=11&postid=29423933740

    Also, can we all finally drop the www part of URLs someday? Everyone insists on using it, but it’s unnecessary. (I do see that you force it here! :-P)

    My favorite examples of needlessly ugly URLs are just about any Slashdot URL, and the wiki URLs from the mismanaged TV Tropes website. This is the canonical TV Tropes main page URL (you get redirected here if you try just the domain),

    http://tvtropes.org/pmwiki/pmwiki.php/Main/HomePage

    It contains “pmwiki” not once, but twice, and probably shouldn’t have it visible at all. The entire “pmwiki/pmwiki.php/Main/” is a big static chunk of ugly. Hey, no www though! And it looks like a file hierarchy. So, there are some good things here. I think any of these would be much better.

    [list removed for spam filter, use your imagination]

    (I had more stuff written, but there is some kind of URL limit, then your spam filter kicks on. Or it doesn’t allow lists of URLs. We are losing the spam war to crappy spam filters.)

    Ok, I’m done ranting!

    Reply  |  Quote
  2. Matt` UNITED KINGDOM Mozilla Firefox Windows Terminalist says:

    Some day I am going to want to do things, and the archives of this website will be crazy-useful.

    So don’t you do anything crazy like going offline, I might need all those code examples for doing useful thing X in language Y :P

    Reply  |  Quote
  3. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    Wait… Where did my comment go? I swear I posted a comment just a second ago. WTF? Oh well, I guess I didn’t press Post or something. Anyway…

    @Chris Wellons: Yeah, I force it here, but I think I did it because of some legitimate reason at the time. I think it had something to do with page rank for www URL being higher than for the non http://www... Still, it shouldn’t matter if I just 301 redirect everything.

    Just for shits and giggles I tried forcing the non-www version of the URL and WordPress went into some sort of self-redirecting loop. So I’m guessing something is broken somewhere and I need to locate and fix it before I do the switch.

    Yes, I agree it is unnecessary and usually when I link back to my blog I write the URL without WWW.

    @Matt`: No worries. If the blog goes offline it’s usually Dreamhost fucking up, not me. I’m usually very cautious about not screwing around with settings that are working well for no reason. Well, except today when I did mess with them and knocked the site down for about 3 minutes. :P

    Reply  |  Quote
  4. Pingback: Google App EngineでURLのRewrite – ssonLogger JAPAN WordPress

  5. redforest MALAYSIA Mozilla Firefox Windows says:

    Very simple and useful hack, thanks for post =)

    Reply  |  Quote
  6. John UNITED KINGDOM Mozilla Firefox Windows says:

    does anyone know how to do this in java app engine?

    Reply  |  Quote
  7. John UNITED KINGDOM Mozilla Firefox Windows says:

    had to add another comment as i forgot to check the “notify me” tickbox!

    Reply  |  Quote
  8. wessite BELGIUM Google Chrome Mac OS says:

    Thanks for the post. I need this on my new app.

    The www part is still useful for the speed freaks out there, check here: http://developer.yahoo.com/performance/rules.html#cookie_free

    Reply  |  Quote
  9. A Hamid BANGLADESH Mozilla Firefox Windows says:

    Does anyone know how to do this with java and GAE?

    Reply  |  Quote
  10. Soan INDIA Google Chrome Windows says:

    Can you show the code for doing a URL rewrite using JAVA code on Google App. Engine?
    I am trying to create a proxy for DropBox using Google App. Engine and want to rewrite the URLs.

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *