I'm running a static site on GAE and using a custom domain (let's call it `example.com`) with SSL certificates enabled. I'd like to canonicalize URLs to `https://www.example.com/`. That means catching any requests to `myproject.appspot.com`, plain HTTP, and/or the naked domain, and redirecting to `www` over HTTPS.

I understand that it's not possible to put redirect logic in `app.yaml`, but ideally I'd like to keep the static file serving logic there, and only have app code for the redirect. (As opposed to doing the static serving in app code as well.)

Here's what I have so far:

Contents of the file `app.yaml`:

```
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /
  static_files: www/index.html
  upload: www/index.html

- url: /(.*)
  static_files: www/\1
  upload: www/(.*)

```

Contents of the file `dispatch.yaml`:

```
dispatch:
- url: "myproject.appspot.com/*"
  module: canonicalizer

```

Contents of the file `canonicalizer.yaml`:

```
module: canonicalizer
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: canonicalizer.app

```

Contents of the file `canonicalizer.py`:

```
import webapp2

def get_redirect_uri(handler, *args, **kwargs):
    return 'https://www.example.com/' + kwargs.get('path')

app = webapp2.WSGIApplication([
    webapp2.Route('/<path:.*>',
    webapp2.RedirectHandler,
    defaults={'_uri': get_redirect_uri, '_code': 302}),
], debug=True)

```

As you can see, I've only attempted to implement redirecting `myproject.appspot.com` so far. I haven't been able to get it working; `myproject.appspot.com` still serves content rather than redirecting to the custom domain.

I saw [a similar SO question](https://stackoverflow.com/questions/32671890/redirect-all-requests-from-one-domain-to-another-with-google-app-engine-but-keep) and used it as a basis for my code above. I followed it fairly closely, so I'm not sure if it's outdated or missing details.

I'm not very familiar with webapp2. Also open to solutions in a different framework or even different programming language.