django-canonical-domain - Canonical domain redirection for Django¶
Version 0.11-1-ga9092e2
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.
Installation and usage¶
pip install django-canonical-domainAdd
canonical_domaintoINSTALLED_APPSandcanonical_domain.middleware.canonical_domainto yourMIDDLEWAREsetting. Ensure that you add this middleware beforedjango.middleware.security.SecurityMiddleware.Set
SECURE_SSL_HOST = 'example.com'in your settings.Optionally set
SECURE_SSL_REDIRECT = Trueif you want to enforce HTTPS.django-canonical-domainalso respectsSECURE_REDIRECT_EXEMPTsettings. In the case path matches the regex the url will be redirected toSECURE_SSL_HOST, but the protocol will not be changed.
Configuration¶
CANONICAL_DOMAIN_EXEMPT
Default: None
A function which receives the request object and returns True if the
request shouldn’t be redirected to the canonical domain. HTTPS will still be
enforced when SECURE_SSL_REDIRECT is set.
If you want to allow api.example.com in addition to example.com you
could set CANONICAL_DOMAIN_EXEMPT = lambda request: request.get_host() ==
"api.example.com". The setting also supports a dotted Python path to a
function.
Change log¶
Next version¶
0.11 (2024-08-09)¶
Changed
CANONICAL_DOMAIN_EXEMPTto be a function or a dotted Python path to a function instead of a list of regexes. It’s more flexible.
0.10 (2024-08-08)¶
Added the requirement that
canonical_domainbe added toINSTALLED_APPSbecause otherwise the system checks wouldn’t run at all.Added deploy checks which verify that
SECURE_SSL_HOSTandSECURE_SSL_REDIRECThave been set.Added Django 5.1 to the CI matrix.
Added support for Django’s
SECURE_REDIRECT_EXEMPTsetting and support for adding additional hosts which shouldn’t be redirected. Thanks @PetrDlouhy!
0.9 (2022-01-31)¶
Added pre-commit.
Dropped support for Django < 3.2, Python < 3.8, added support for Django 4.0 and Python 3.10.
Switched back to using
SECURE_SSL_REDIRECTandSECURE_SSL_HOST– introducing our own settings made it necessary to silence Django’s system checks for them. Let’s not do that anymore. Added system checks to ensure that the new settings are added correctly.Added a system check which verifies that the canonical domain middleware appears before the security middleware in
MIDDLEWARE.
0.4 (2021-07-20)¶
Dropped official support for Django < 2.2 and Python < 3.6.
Switched to a declarative setup.
Switched to GitHub actions.
Replaced the
CanonicalDomainMiddlewarewith a new function-basedcanonical_domainmiddleware which does not extendSecurityMiddleware. You should addSecurityMiddlewareyourself and remove the silencing ofsecurity.W001.
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,OPTIONSandTRACE).
0.2 (2017-07-12)¶
Merged
CanonicalDomainMiddlewareandSecurityCanonicalDomainMiddlewareand added a new settingCANONICAL_DOMAIN_SECUREwhich replaces our usage ofSECURE_SSL_REDIRECT.Added some documentation.
Fixed links in the changelog.
0.1 (2017-07-12)¶
Initial public version.