'Redirect http to https in Django (using sslserver)
I have a django project working with HTTPS using django sslserver.I want http
to be redirected to https
. I tried adding SECURE_SSL_REDIRECT = True
which does not seem to have any effect.
Similarly to test if my redirection was right, I tried the following on a test project.
- Created a new django project
- Added
SECURE_SSL_REDIRECT = True
to thesettings.py
file. Here now, when I try to run the server withhttp
it redirects tohttps
. But asof now my server does not supporthttps
cause of which desired web page is not displayed. - Installed
sslserver
- Ran the project with the command
python manage.py runsslserver 8000
- This succesfully redirects the webpage to
https
even if I open the url withhttp
This test redirection works fine like this. But if I already have sslserver
installed ssl redirection does not seem to have any effect. I have been stuck with this problem for a while now and would really appreciate some help.
Solution 1:[1]
SECURE_SSL_REDIRECT
settings works together with SecurityMiddleware
.
Try add it to MIDDLEWARE_CLASSES
in settings.py
.
Solution 2:[2]
In addition to SECURE_SSL_REDIRECT=True in settings.py as mentioned, you need both
- runsslserver on port 443
- runserver on port 80
To have another service listen on port 80 for incoming http requests and do the redirect.
That works for me.
Solution 3:[3]
try adding to your settings.
ACCOUNT_DEFAULT_HTTP_PROTOCOL = 'https'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vladimir Osintsev |
Solution 2 | jsu |
Solution 3 | Mint |