/django-enforce-host

Middleware to redirect requests to a canonical host

Primary LanguagePythonBSD 2-Clause "Simplified" LicenseBSD-2-Clause

django-enforce-host

Build Status pypi release

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 1.8, 1.9, 1.10, 1.11 on Python 2.7, 3.4 and 3.6

Installation

Install from PIP

pip install django-enforce-host

In your MIDDLEWARE setting, add the following at the top:

MIDDLEWARE = [
    '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!