The redirects app#
GingerDJ comes with an optional redirects application. It lets you store
redirects in a database and handles the redirecting for you. It uses the HTTP
response status code 301 Moved Permanently by default.
Installation#
To install the redirects app, follow these steps:
Ensure that the
gingerdj.contrib.sitesframework is installed.Add
'gingerdj.contrib.redirects'to yourINSTALLED_APPSsetting.Add
'gingerdj.contrib.redirects.middleware.RedirectFallbackMiddleware'to yourMIDDLEWAREsetting.Run the command
manage.py migrate.
How it works#
manage.py migrate creates a ginger_redirect table in your database. This
is a lookup table with site_id, old_path and new_path fields.
The RedirectFallbackMiddleware
does all of the work. Each time any GingerDJ application raises a 404
error, this middleware checks the redirects database for the requested
URL as a last resort. Specifically, it checks for a redirect with the
given old_path with a site ID that corresponds to the
SITE_ID setting.
If it finds a match, and
new_pathis not empty, it redirects tonew_pathusing a 301 (“Moved Permanently”) redirect. You can subclassRedirectFallbackMiddlewareand setresponse_redirect_classtogingerdj.http.HttpResponseRedirectto use a302 Moved Temporarilyredirect instead.If it finds a match, and
new_pathis 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 as usual.
The middleware only gets activated for 404s – not for 500s or responses of any other status code.
Note that the order of MIDDLEWARE matters. Generally, you can put
RedirectFallbackMiddleware at the
end of the list, because it’s a last resort.
For more on middleware, read the middleware docs.
How to add, change and delete redirects#
Via the admin interface#
If you’ve activated the automatic GingerDJ admin interface, you should see a “Redirects” section on the admin index page. Edit redirects as you edit any other object in the system.
Via the Python API#
- class models.Redirect#
Redirects are represented by a standard GingerDJ model, which lives in gingerdj/contrib/redirects/models.py. You can access redirect objects via the GingerDJ database API. For example:
>>> from gingerdj.conf import settings >>> from gingerdj.contrib.redirects.models import Redirect >>> # Add a new redirect. >>> redirect = Redirect.objects.create( ... site_id=1, ... old_path="/contact-us/", ... new_path="/contact/", ... ) >>> # Change a redirect. >>> redirect.new_path = "/contact-details/" >>> redirect.save() >>> redirect <Redirect: /contact-us/ ---> /contact-details/> >>> # Delete a redirect. >>> Redirect.objects.filter(site_id=1, old_path="/contact-us/").delete() (1, {'redirects.Redirect': 1})
Middleware#
- class middleware.RedirectFallbackMiddleware#
You can change the
HttpResponseclasses used by the middleware by creating a subclass ofRedirectFallbackMiddlewareand overridingresponse_gone_classand/orresponse_redirect_class.- response_gone_class#
The
HttpResponseclass used when aRedirectis not found for the requested path or has a blanknew_pathvalue.Defaults to
HttpResponseGone.
- response_redirect_class#
The
HttpResponseclass that handles the redirect.Defaults to
HttpResponsePermanentRedirect.