Python

Redirecting URLs in Google AppEngine for Python

A month ago, I decided to re-structure my URLs but have no plan on handling redirects for indexed URLs by search engines. I’ve been receiving several 404 errors on my logs and finally decided to do a 301 redirect to the new URL.

Overview

The URLs I wanted to redirect starts with /extra/tools as the base URL and anything that follows are sub-pages. For example, my previous URL looks like below:

http://www.lysender.com/extra/tools/urlencode

and now I wanted to redirect it into a new URL below:

http://www.lysender.com/tools/urlencode

The code

With this redirection, I opted to create a few routes. The routes are the following:

  • /extra/tools – for the tools index page
  • /extra/tools/ – for the tools index page with a trailing slash
  • /extra/tools/ – for anything beyond the tools base URL

Below are the actual codes:

webapp2.Route(r'/extra/tools/<ident1:[-+0-9a-zA-Z_/]+>', handler='dclab.lysender.handler.extra.redirect.RedirectHandler:tools_nonindex', name='redir_tools_nonindex', methods=['GET']),
webapp2.Route(r'/extra/tools/', handler='dclab.lysender.handler.extra.redirect.RedirectHandler:tools_index', name='redir_tools_index', methods=['GET']),
webapp2.Route(r'/extra/tools', handler='dclab.lysender.handler.extra.redirect.RedirectHandler:tools_index', name='redir_tools_index2', methods=['GET']),

Next, I decided to have a single redirection script/handler that will handle future redirections. For every URL, a method will take care of it. For my situation, I have two methods to handle the following:

  • Tools index page
  • Tools sub pages

Below is the code for my redirection script handler.

import os
import string
import urllib
import yaml
import json
import webapp2
from dclab.handler.web import WebHandler

class RedirectHandler(WebHandler):
    def tools_index(self):
        self.redirect(webapp2.uri_for('tools_index', _full=True), True)

    def tools_nonindex(self, **kwargs):
        redirect_url = '%stools/%s' % (webapp2.uri_for('index', _full=True), kwargs['ident1'])
        self.redirect(redirect_url, True)

Please don’t bother the several useless library I’ve imported. They are copy and paste thing and I’ve no plan on updating them on live anyway, maybe on next deployment. Here we go. I have two methods, tools_index and tools_nonindex. They will simply redirect to the new URL on /tools/* and for SEO purposes, I do a 301 permanent redirect. Of course this is a permanent redirect.

self.redirect(redirect_url, True)

The second parameter is optional. By default it is False and the redirect is a 302 temporary redirect. When we pass True, it will do a 301 permanent redirect.

It should reduce my error logs from now on.

Leave a reply

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