How to redirect all traffic from http to https for Django 3 apps hosted on Heroku

Eli Williams
1 min readJan 4, 2021

--

I’ve come across the following issue a handful of times when building new web apps hosted on Heroku: I want a custom domain, and I want a simple way to force a http → https redirect.

If you set up a custom URL on Heroku for your Django app and you pay for hobby or higher dynos (SSL is not available on the free level), then follow the below step to enable a redirect from insecure http requests to secure https requests.

From within your Django app, add the following to your settings.py file:

...MIDDLEWARE = [
# SecurityMiddleware must be listed before other middleware
'django.middleware.security.SecurityMiddleware',
# ...
]
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT = True
...

That’s it! Push your app to Heroku and confirm that http requests now redirect to https.

If you liked this, follow me on Twitter @elitwilliams.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Eli Williams
Eli Williams

Written by Eli Williams

Crypto Market Making at @SQSTrading

Responses (1)

Write a response