Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dabapps/django-enforce-host
Middleware to redirect requests to a canonical host
https://github.com/dabapps/django-enforce-host
Last synced: 4 days 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 (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-03-20T16:58:57.000Z (9 months ago)
- Last Synced: 2024-12-06T01:17:33.482Z (21 days ago)
- Language: Python
- Homepage:
- Size: 30.3 KB
- Stars: 17
- Watchers: 21
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
django-enforce-host
===================[![pypi release](https://img.shields.io/pypi/v/django-enforce-host.svg)](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/)