It is desirable to redirect users to https for some pages.
https itself seems to work fine.
Is there a way to make an url generated by url_for() an https url?
It is desirable to redirect users to https for some pages.
https itself seems to work fine.
Is there a way to make an url generated by url_for() an https url?
I'm not a Flask expert (you mean Flask's url_for()
, right?) but I think it returns the path portion of the URL -- this part of the quickstart guide seems to imply that, anyway. So to make it https, you can just prepend https://lst.pythonanywhere.com
to it.
You might find this application wrapper useful. As it stands, it redirects all HTTP requests to HTTPS. It registers a before_request
handler which does an early return of a redirect if appropriate. You may not wish to use it directly, but hopefully it'll serve as a useful example of how to achieve what you're looking for.
Of course, if you only want to redirect a subset of URLs, you'd need to modify that to add some additional criteria to the redirect_to_ssl()
function - for example, I like to keep all my pages which require SSL under a "/secure" portion of the URL space to make it easy to enforce, but that's a matter of personal taste.
In case anyone else finds it useful, we have a guide to automatically redirecting http to https.