Google App Engine Static Website Redirect Trailing Slash

Routing directory index page without trailing slash to one with trailing slash

This page is about static website hosting and redirect pages like /about to /about/.

Sadly, redirect configuration is not possible using app.yaml, but it can be used with a combination of coding to achieve the same result.

Edit app.yaml.

handlers:# file with extensions (longer cache period)- url: /(.*\.(css|js|woff|woff2|ico|png))  static_files: www/\1  upload: www/(.*)  expiration: "7d"  secure: always# file with other extensions (e.g. .html)- url: /(.*\..*)  static_files: www/\1  upload: www/(.*)  secure: always# assume file without extensions use default index.html- url: /(.*)/  static_files: www/\1/index.html  upload: www/(.*)/index.html  secure: always# home- url: /  static_files: www/index.html  upload: www/index.html  secure: always# access without trailing slash- url: /(.*)  script:

Create file mini/

This file will redirect pages like /about to /about/.

from flask import Flask, abortapp = Flask(__name__)@app.route('/<path:path>/')def redirect(path):    # will never reach this code, as /path/ is intercepted at app.yaml    abort(404)

NOTE: I make use of flask routing feature to redirect pages without trailing slash to one with trailing slash, but you could use other methods which you are comfortable with.

NOTE: Refer to Flask On Google App Engine

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.