0

I my Flask app to redirect http to https. I found python flask redirect to https from http but it does not work.

from flask import Flask, request, redirect from werkzeug.serving import make_ssl_devcert make_ssl_devcert('key') app = Flask(__name__) @app.before_request def before_request(): if request.url.startswith('http://'): url = request.url.replace('http://', 'https://', 1) code = 301 return redirect(url, code=code) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run(host='127.0.0.1', port=443, debug=False, ssl_context=('key.crt', 'key.key')) 

1 Answer 1

3

First, this should better be done by nginx or whatever you are using in front of flask

For your question, you are listening only on the 443 port not the 80, which is the one http uses, so the http request does not actually hit your server.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.