You cannot do this using ssl_context and Werkzung (the default Flask server). Functionality allowing this was proposed and rejected in 2014: auto http to https redirect ; quoting:
This requires starting another HTTP server. Werkzeug is not capable of this, and IMO it goes beyond that. run_simple should be used only for development.
So what happens, your Flask application calls run_simple under it, passing ssl_context and some other variables. SSLify does not affect your routing as long as you use ssl_context , since the only presence of this variable makes Werkzung a host using only the https scheme. To get a redirect from http to https, you need to either configure another server, listen on HTTP and redirect to https, or transfer it to another, more advanced server, which redirects easily.
I recommend switching to Apache or gunicorn. Flask provides comprehensive deployment instructions: Deployment Options . Keep in mind that the Flask embedded server (Werkzung) is not suitable for production, as the Flask authors write:
While lightweight and easy to use, the Flocks built-in server is suitable for production, as it does not scale well and by default only serves one request at a time.
Using Apache, you can redirect all HTTP requests using the VirtualHost rule by listening to 80:
<VirtualHost *:80> ServerName mysite.example.com DocumentRoot /usr/local/apache2/htdocs Redirect /secure https://mysite.example.com/secure </VirtualHost>
For more information, see Redirect the request to SSL on the Apache Wiki.
source share