How to redirect all URLs with Google App Engine

0 votes
asked Jun 29, 2009 by mistero

How do I configure the app.yaml file to redirect all URLs to another URL? For example I want http://test.appspot.com/hello or http://test.appspot.com/hello28928723 to redirect to http://domain.com.

I am only serving static files at the moment. Here is my app.yaml file:

application: testapp
version: 1
runtime: python
api_version: 1

handlers:
- url: (.*)/
  static_files: static\1/index.html
  upload: static/index.html

- url: /
  static_dir: static

7 Answers

0 votes
answered Jun 29, 2009 by diega

you can redirect all requests easily with a python handler. Something like

class FormHandler(webapp.RequestHandler):
  def post(self):
    if processFormData(self.request):
      self.redirect("http://domain.com")
0 votes
answered Jan 1, 2012 by brian-leathem

Here is a python script that will do the redirect:

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

class MainPage(webapp.RequestHandler):
  def get(self, path):
    self.redirect("http://example.com", permanent=True)
  def head(self, path):
    self.redirect("http://example.com", permanent=True)

application = webapp.WSGIApplication(
            [
                (r'^(.*)', MainPage)
            ])

def main():
   run_wsgi_app(application)

if __name__ == "__main__":
    main()
0 votes
answered Jan 24, 2012 by evan-plaice

Webapp2 has a built-in redirect handler

No need to roll your own handler; webapp2 already comes with one.

application = webapp2.WSGIApplication([
    webapp2.Route('/hello', webapp2.RedirectHandler, defaults={'_uri':'http://domain.com'}),
    webapp2.Route('/hello28928723', webapp2.RedirectHandler, defaults={'_uri':'http://domain.com'}),
], debug=False)

The _uri argument is what the RedirectHandler class uses to define the destination. It took a lot of Google Fu to find the documentation on this but it works flawlessly in my app.

Update:

I assumed you're aware of this but you need to change your catch-all route from:

- url: /
  static_dir: static

To (python27 version):

- url: /.*
  script: main.application

Or: (pre python27 version)

- url: /.*
  script: main.py

main.py is the file containing the request handler + routes.

Note: There is no static-only way to handle redirects on GAE because of the nature of static files. Basically, there's no way to do a redirect in app.yaml alone.

0 votes
answered Jan 18, 2013 by feklee

All you need (replace app-id, http://example.com):

  • app.yaml:

    application: app-id
    version: 1
    runtime: python27
    api_version: 1
    threadsafe: false
    
    handlers:
    - url: /.*
      script: main.py
    
  • main.py:

    from google.appengine.ext import webapp
    from google.appengine.ext.webapp.util import run_wsgi_app
    
    class AllHandler(webapp.RequestHandler):
        def get(self):
            self.redirect("http://example.com", True)
    
    application = webapp.WSGIApplication([('/.*', AllHandler)])
    
    def main():
        run_wsgi_app(application)
    
    if __name__ == "__main__":
        main()
    
0 votes
answered Jan 6, 2014 by ron

If you want a static-files-only way to do "redirects" then do this:

In the app.yaml, put this as the catch-all, at the end of the file:

-   url: /.*
    static_files: root/redirect.html
    upload: root/redirect.html

Then in the root/redirect.html file, put this:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="refresh" content="0;URL=/" />
        <script>
            location.replace("/");
        </script>
    </head>
    <body></body>
</html>

This example will redirect all unknown URLs to the root (i.e., / ). If you want another URL, just substitute http://mydomain.com in the appropriate places.

0 votes
answered Jan 3, 2015 by qix

Riffing off Evan's answer, to redirect all requests you could use regexes to do something like:

import webapp2
from webapp2_extras.routes import RedirectRoute

app = webapp2.WSGIApplication([
     RedirectRoute('/<:.*>', redirect_to='/')
    ], debug=False)

For official docs check out:

https://webapp-improved.appspot.com/api/webapp2_extras/routes.html

https://webapp-improved.appspot.com/api/webapp2.html#webapp2.Route.init

0 votes
answered Sep 15, 2017 by conor-muldoon

The redirect handler for webapp2 (webapp2.RedirectHandler) mentioned in a previous answer will not work for post requests, as it does not contain a post method (see https://github.com/GoogleCloudPlatform/webapp2/blob/master/webapp2.py), so you will need to roll your own python handler if concerned with posts. Something like the following:

import webapp2

class MainPage(webapp2.RequestHandler):


    def post(self):
        self.redirect('http://example.com')


application = webapp2.WSGIApplication([
('/.*', MainPage)
], debug=False)
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...