Trailing slash in Flask route

You are on the right tracking with using strict_slashes, which you can configure on the Flask app itself. This will set the strict_slashes flag to False for every route that is created

app = Flask('my_app')
app.url_map.strict_slashes = False

Then you can use before_request to detect the trailing / for a redirect. Using before_request will allow you to not require special logic to be applied to each route individually

@app.before_request
def clear_trailing():
    from flask import redirect, request

    rp = request.path 
    if rp != "https://stackoverflow.com/" and rp.endswith("https://stackoverflow.com/"):
        return redirect(rp[:-1])

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)