Google App Engine Static Website Redirect Trailing Slash
September 29, 2018Routing 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: mini.app
Create file mini/__init__.py
.
This file will redirect pages like /about
to /about/
.
from flask import Flask, abort
app = 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
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn