Tay Ray Chuan home archive

support for canonical URLs in bloggart

Sat, 22 May 2010 02:15:36 +0800 | Filed under bloggart

I was scratching my head on how to do this, now that mod_rewrite (Apache or nginx or otherwise) isn't available.

Some googling threw up this post. Myself, I felt that using os.environ, although not incorrect, wasn't very pretty.

Here's a decorator that uses webapp.Request instead:

def canonical_redirect(func):
  def _dec(self, path):
    if not self.request.host == config.host:
      self.redirect("%s://%s%s" % (self.request.scheme, config.host, path), True)
      func(self, path)
  return _dec

I also choose to apply this only to the static handler in static.py, since that's where most "public" requests for the site will go to.

Get it in your bloggart setup in my rc/canonical branch.

blog comments powered by Disqus