django-canonical-domain - Canonical domain redirection for Django

Version 0.3-3-gafa3d27

This module allows redirecting all requests for a given Django instance to a single canonical domain and optionally enforcing HTTPS for all requests as well.

It achieves this by providing a replacement for Django’s which overrides its request processing with a variant that also redirects requests to the canonical domain which already are secure.

Installation and usage

  • pip install django-canonical-domain
  • Add canonical_domain.middleware.CanonicalDomainMiddleware to your MIDDLEWARE setting (or MIDDLEWARE_CLASSES if you still are on a old school Django version)
  • Set CANONICAL_DOMAIN = '' in your settings.
  • Optionally set CANONICAL_DOMAIN_SECURE = True if you want to enforce HTTPS.
  • Set additional SECURE_* settings – all settings are valid and supported except for SECURE_SSL_REDIRECT and SECURE_SSL_HOST.

Change log

Next version

  • Dropped official support for Django < 2.2 and Python < 3.6.

0.3 (2018-12-24)

  • Removed the self-deactivation of the middleware when DEBUG = True.
  • Reformatted the code using black.
  • Changed the middleware to only redirect safe methods (GET, HEAD, OPTIONS and TRACE).

0.2 (2017-07-12)

  • Merged CanonicalDomainMiddleware and SecurityCanonicalDomainMiddleware and added a new setting CANONICAL_DOMAIN_SECURE which replaces our usage of SECURE_SSL_REDIRECT.
  • Added some documentation.
  • Fixed links in the changelog.

0.1 (2017-07-12)

  • Initial public version.