file test.py
from flask import Flask, request
from werkzeug.utils import append_slash_redirect
app = Flask("test")
@app.route('/<path:path>')
def r(path):
if not path.endswith('/'):
return append_slash_redirect(request.environ)
return path
run FLASK_APP=test.py flask run
A request for http://localhost:5000/parent/child gives a redirect that browsers interpret as http://localhost:5000/parent/parent/child/
Expecting a redirect that browsers interpret as http://localhost:5000/parent/child/
Possible solution:
The code strips the front '/' which leads to this situation. i.e. the returned location header is relative: 'parent/child/'
Use environ["PATH_INFO"].rstrip("/") + "/" instead
file
test.pyrun
FLASK_APP=test.py flask runA request for http://localhost:5000/parent/child gives a redirect that browsers interpret as
http://localhost:5000/parent/parent/child/Expecting a redirect that browsers interpret as
http://localhost:5000/parent/child/Possible solution:
The code strips the front '/' which leads to this situation. i.e. the returned location header is relative: 'parent/child/'
Use
environ["PATH_INFO"].rstrip("/") + "/"instead