The redirects app
To install the redirects app, follow these steps:
- Add
'django.contrib.redirects'
to yourINSTALLED_APPS
setting. - Add
'django.contrib.redirects.middleware.RedirectFallbackMiddleware'
to your setting. - Run the command
manage.py migrate
.
manage.py migrate
creates a django_redirect
table in your database. Thisis a simple lookup table with site_id
, old_path
and new_path
fields.
- If it finds a match, and
new_path
is not empty, it redirects tonew_path
using a 301 ("Moved Permanently") redirect. You can subclassand setresponse_redirect_class
to to use a302 Moved Temporarily
redirect instead. - If it finds a match, and
new_path
is empty, it sends a 410 ("Gone")HTTP header and empty (content-less) response. - If it doesn't find a match, the request continues to be processed asusual.
The middleware only gets activated for 404s — not for 500s or responses of anyother status code.
Note that the order of MIDDLEWARE
matters. Generally, you can put at theend of the list, because it's a last resort.
For more on middleware, read the middleware docs.
Via the Python API
- Redirects are represented by a standard Django model,which lives in . You can accessredirect objects via the Django database API.
- class
RedirectFallbackMiddleware
You can change the
HttpResponse
classes usedby the middleware by creating a subclass ofand overridingresponse_gone_class
and/orresponse_redirect_class
.response_gone_class
- The class used when a
Redirect
is not found for therequested path or has a blanknew_path
value.
Defaults to .