https://github.com/dabapps/django-enforce-host
Middleware to redirect requests to a canonical host
https://github.com/dabapps/django-enforce-host
Last synced: 3 months ago
JSON representation
Middleware to redirect requests to a canonical host
- Host: GitHub
- URL: https://github.com/dabapps/django-enforce-host
- Owner: dabapps
- License: bsd-2-clause
- Created: 2017-11-24T15:31:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-03-20T16:58:57.000Z (over 1 year ago)
- Last Synced: 2025-04-06T11:47:13.189Z (3 months ago)
- Language: Python
- Homepage:
- Size: 30.3 KB
- Stars: 17
- Watchers: 20
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
django-enforce-host
===================[](https://pypi.python.org/pypi/django-enforce-host)
Sometimes, it's unavoidable that multiple URLs point at the same app - for example, on Heroku, all apps get a `.herokuapp.com` address, as well as any custom domains that are pointed at them.
This is a simple Django middleware that redirects all traffic from hosts other than the one(s) you specify to your canonical URL.
Tested against Django 3.2 and 4.0, 4.1 and 4.2 on Python 3.8, 3.9, 3.10 and 3.11.
### Installation
Install from PyPI
pip install django-enforce-host
In your `MIDDLEWARE` setting, add the middleware just after the `SecurityMiddleware`:
MIDDLEWARE = [
# Django's SecurityMiddleware will handle HTTP -> HTTPS redirects
# before the EnforceHostMiddleware redirects to the canonical domain
'django.middleware.security.SecurityMiddleware',
'enforce_host.EnforceHostMiddleware',
... other middleware
]Set the following setting to be either a single allowed host, or a list of allowed hosts:
ENFORCE_HOST = 'yourapp.com'
That's it!
## Code of conduct
For guidelines regarding the code of conduct when contributing to this repository please review [https://www.dabapps.com/open-source/code-of-conduct/](https://www.dabapps.com/open-source/code-of-conduct/)