Maximillian Laumeister
Illustration of Miku the bird-fox shouting through a megaphone while flying in the sky

How to use Google App Engine as a free redirect server

This guide will show you how to set up a redirect app on Google App Engine that uses a mapping of input URLS to output URLs to redirect the user depending on the path on your website that they visited.

Update December 2018: I now use Netlify for my own redirects because it’s simpler than maintaining a Google App Engine project. For another good service to do redirects, see redirects in Google Firebase Hosting.

In some cases it’s useful to have a redirect from one of your domains to another. Many hosting sites allow you to set up a site-wide 302 (temporary) or 301 (permanent) redirect from their control panel, but most only allow either removing the URL path or forwarding it as-is. This means that we could redirect to either (with the path intact) or (with the path removed). Using the Google App Engine technique outlined in this article, we can map any arbitrary URL to any other arbitrary URL, for example whenever a user visits, they could be redirected to or The cool thing about using Google App Engine for this is that just by serving redirects, we are very unlikely to hit the 1GB daily free outgoing bandwidth quota for App Engine, so the redirect service will be free!

In my case, I’m using this technique as a basic personal URL shortener - for example I can put the short URL on my resume, and when the user types in the URL, they get redirected to the relevant section on my main site, at I could also see this technique being used in the case where a user needs more forwarding customization options than their hosting provider gives them, or if they want to redirect requests from one old domain to one of several new domains. So now that you have an idea of when you might want to use this code, let’s jump in!

The Code

All we need for this setup is two files - app.yaml is the configuration file for the app, and is the actual redirect script.


Below is the app.yaml config file. This file tells Google App Engine the ID of our app and what scripts to use to serve requests:

runtime: python27

application: your-app-name-here
version: 1
api_version: 1
threadsafe: no

default_expiration: "6h"

- url: /.*

In the above config file, the handlers section specifies that we want every request to the app to be handled by our one main script, so the file will do all of the work.

Below is the script that does the actual redirects. It’s been adapted from a script written by Dan Tuppeny.

import webapp2
import logging
from urlparse import urlparse

# Path => URL to redirect to, redirect type (301 is permanent, 302 is temporary)
urls = {
    '/': ('', '302'), # <-- EDIT THESE ENTRIES
    '/a': ('', '301'), # <--
    '/cs': ('', '301'), # <--
    '/treo': ('', '301'), # <--
    '/hex': ('', '301'), # <--
    '/puff': ('', '301') # <--

# If the URL doesn't match any above, a 302 redirect will be done to the following address

def get_redirect_url(url):
    scheme, netloc, path, params, query, fragment = urlparse(url)

    # Fix empty paths to be just '/' for consistency
    if path == '':
        path = '/'

    # Check if we have a mapping for this url
    if path in urls:
        return urls[path]
        return None, None

class MainPage(webapp2.RequestHandler):
    def get(self):
        # Perform redirect
        url, perm = get_redirect_url(self.request.url)

        if url:
            self.redirect(url, permanent= (perm == '301') )

            # Log that we didn't know what this was, and redirect to a good default
            logging.error('Unable to redirect this url: ' + self.request.url);

            # Don't do permanent (301), since we don't know what this is.
            self.redirect(DEFAULT_URL, permanent=False)

app = webapp2.WSGIApplication([
    ('/.*', MainPage),

I have this redirect script running on the domain, so for example will be 302-redirected to, and will be 301-redirected to A default URL is also specified (in the DEFAULT_URL variable), so if the user visits a path that is not recognized by the script, they will be 302-redirected to a default landing page.

Uploading the Code

Now that we have the necessary files, we need to create a project for them and upload them to Google App Engine so they start serving!

  1. Download the app.yaml and files mentioned above and put them in a new folder on your computer. For our example we will use a folder in our home folder called redirect-app.
  2. Edit the urls dictionary in (where it says “<– EDIT THESE ENTRIES”) to match your own redirect situation. The input paths are on the left, and the destination URLS are on the right. Also edit the DEFAULT_URL to specify a default redirect location for if none of the paths are matched.
  3. Visit the Google Cloud Platform Console and click Create Project. Enter a desired project name and a desired project ID.
  4. Download and install the Google App Engine SDK for Python.
  5. Using the tool from the SDK, run the following command: -A YOUR_PROJECT_ID update ~/redirect-app/

    Replace YOUR_PROJECT_ID with the project ID that you created in step 3, and ~/redirect-app/ with the full path to the folder containing the scripts you downloaded in step 1. Your redirect app should now be available at

  6. You will likely want to run your redirect app on a custom domain, so attach your own domain by following Google’s instructions on using custom domains and ssl.


You should now have a Google App Engine app that redirects traffic from URLs on your old domain to whatever destinations you want. And remember that as long as your outgoing bandwidth doesn’t exceed the free quota of 1GB, your app will run for free. Special shout-out to Dan Tuppeny for his code and article that helped shape this one. Hopefully this article was helpful, and please leave any thoughts in the comments!

More Articles Tagged #tech