{"title":"The redirects app","version":"6.1","locale":"en","docname":"ref/contrib/redirects","url":"/en/6.1/ref/contrib/redirects/","canonical":"https://djangodocs.dev/en/6.1/ref/contrib/redirects/","summary":"Django 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…","html":"<span id=\"the-redirects-app\"></span><h1>The redirects app<a class=\"heading-anchor\" href=\"#module-django.contrib.redirects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django comes with an optional redirects application. It lets you store\nredirects in a database and handles the redirecting for you. It uses the HTTP\nresponse status code <code class=\"docutils literal notranslate\"><span class=\"pre\">301</span> <span class=\"pre\">Moved</span> <span class=\"pre\">Permanently</span></code> by default.</p>\n<section id=\"installation\">\n<h2>Installation<a class=\"heading-anchor\" href=\"#installation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To install the redirects app, follow these steps:</p>\n<ol class=\"arabic simple\">\n<li><p>Ensure that the <code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.sites</span></code> framework\n<a class=\"reference internal\" href=\"/en/6.1/ref/contrib/sites/#enabling-the-sites-framework\"><span class=\"std std-ref\">is installed</span></a>.</p></li>\n<li><p>Add <code class=\"docutils literal notranslate\"><span class=\"pre\">'django.contrib.redirects'</span></code> to your <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a>\nsetting.</p></li>\n<li><p>Add <code class=\"docutils literal notranslate\"><span class=\"pre\">'django.contrib.redirects.middleware.RedirectFallbackMiddleware'</span></code>\nto your <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a> setting.</p></li>\n<li><p>Run the command <a class=\"reference internal\" href=\"/en/6.1/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">manage.py</span> <span class=\"pre\">migrate</span></code></a>.</p></li>\n</ol>\n</section>\n<section id=\"how-it-works\">\n<h2>How it works<a class=\"heading-anchor\" href=\"#how-it-works\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span> <span class=\"pre\">migrate</span></code> creates a <code class=\"docutils literal notranslate\"><span class=\"pre\">django_redirect</span></code> table in your database.\nThis is a lookup table with <code class=\"docutils literal notranslate\"><span class=\"pre\">site_id</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">old_path</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">new_path</span></code> fields.</p>\n<p>The <a class=\"reference internal\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RedirectFallbackMiddleware</span></code></a>\ndoes all of the work. Each time any Django application raises a 404\nerror, this middleware checks the redirects database for the requested\nURL as a last resort. Specifically, it checks for a redirect with the\ngiven <code class=\"docutils literal notranslate\"><span class=\"pre\">old_path</span></code> with a site ID that corresponds to the\n<a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-SITE_ID\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SITE_ID</span></code></a> setting.</p>\n<ul class=\"simple\">\n<li><p>If it finds a match, and <code class=\"docutils literal notranslate\"><span class=\"pre\">new_path</span></code> is not empty, it redirects to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">new_path</span></code> using a 301 (“Moved Permanently”) redirect. You can subclass\n<a class=\"reference internal\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RedirectFallbackMiddleware</span></code></a>\nand set\n<a class=\"reference internal\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">response_redirect_class</span></code></a>\nto <a class=\"reference internal\" href=\"/en/6.1/ref/request-response/#django.http.HttpResponseRedirect\" title=\"django.http.HttpResponseRedirect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.http.HttpResponseRedirect</span></code></a> to use a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">302</span> <span class=\"pre\">Moved</span> <span class=\"pre\">Temporarily</span></code> redirect instead.</p></li>\n<li><p>If it finds a match, and <code class=\"docutils literal notranslate\"><span class=\"pre\">new_path</span></code> is empty, it sends a 410 (“Gone”)\nHTTP header and empty (content-less) response.</p></li>\n<li><p>If it doesn’t find a match, the request continues to be processed as\nusual.</p></li>\n</ul>\n<p>The middleware only gets activated for 404s – not for 500s or responses of any\nother status code.</p>\n<p>Note that the order of <a class=\"reference internal\" href=\"/en/6.1/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a> matters. Generally, you can put\n<a class=\"reference internal\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RedirectFallbackMiddleware</span></code></a> at the\nend of the list, because it’s a last resort.</p>\n<p>For more on middleware, read the <a class=\"reference internal\" href=\"/en/6.1/topics/http/middleware/\"><span class=\"doc\">middleware docs</span></a>.</p>\n</section>\n<section id=\"how-to-add-change-and-delete-redirects\">\n<h2>How to add, change and delete redirects<a class=\"heading-anchor\" href=\"#how-to-add-change-and-delete-redirects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"via-the-admin-interface\">\n<h3>Via the admin interface<a class=\"heading-anchor\" href=\"#via-the-admin-interface\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you’ve activated the automatic Django admin interface, you should see a\n“Redirects” section on the admin index page. Edit redirects as you edit any\nother object in the system.</p>\n</section>\n<section id=\"via-the-python-api\">\n<h3>Via the Python API<a class=\"heading-anchor\" href=\"#via-the-python-api\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.redirects.models.Redirect\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-prename descclassname\"><span class=\"pre\">models.</span></span><span class=\"sig-name descname\"><span class=\"pre\">Redirect</span></span><a class=\"heading-anchor\" href=\"#django.contrib.redirects.models.Redirect\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Redirects are represented by a standard\n<a class=\"reference internal\" href=\"/en/6.1/topics/db/models/\"><span class=\"doc\">Django model</span></a>, which lives in\n<a class=\"extlink-source reference external\" href=\"https://github.com/django/django/blob/stable/6.1.x/django/contrib/redirects/models.py\">django/contrib/redirects/models.py</a>. You can access redirect\nobjects via the <a class=\"reference internal\" href=\"/en/6.1/topics/db/queries/\"><span class=\"doc\">Django database API</span></a>. For\nexample:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">settings</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.redirects.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Redirect</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># Add a new redirect.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">redirect</span> <span class=\"o\">=</span> <span class=\"n\">Redirect</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">create</span><span class=\"p\">(</span>\n<span class=\"gp\">... </span>    <span class=\"n\">site_id</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"n\">old_path</span><span class=\"o\">=</span><span class=\"s2\">&quot;/contact-us/&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"n\">new_path</span><span class=\"o\">=</span><span class=\"s2\">&quot;/contact/&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># Change a redirect.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">redirect</span><span class=\"o\">.</span><span class=\"n\">new_path</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;/contact-details/&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">redirect</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">redirect</span>\n<span class=\"go\">&lt;Redirect: /contact-us/ ---&gt; /contact-details/&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># Delete a redirect.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Redirect</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">site_id</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"n\">old_path</span><span class=\"o\">=</span><span class=\"s2\">&quot;/contact-us/&quot;</span><span class=\"p\">)</span><span class=\"o\">.</span><span class=\"n\">delete</span><span class=\"p\">()</span>\n<span class=\"go\">(1, {&#39;redirects.Redirect&#39;: 1})</span>\n</code></pre></div>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"middleware\">\n<h2>Middleware<a class=\"heading-anchor\" href=\"#middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-prename descclassname\"><span class=\"pre\">middleware.</span></span><span class=\"sig-name descname\"><span class=\"pre\">RedirectFallbackMiddleware</span></span><a class=\"heading-anchor\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>You can change the <a class=\"reference internal\" href=\"/en/6.1/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> classes used\nby the middleware by creating a subclass of\n<a class=\"reference internal\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware\" title=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RedirectFallbackMiddleware</span></code></a>\nand overriding <code class=\"docutils literal notranslate\"><span class=\"pre\">response_gone_class</span></code> and/or <code class=\"docutils literal notranslate\"><span class=\"pre\">response_redirect_class</span></code>.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_gone_class\">\n<span class=\"sig-name descname\"><span class=\"pre\">response_gone_class</span></span><a class=\"heading-anchor\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_gone_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <a class=\"reference internal\" href=\"/en/6.1/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> class used when a\n<a class=\"reference internal\" href=\"#django.contrib.redirects.models.Redirect\" title=\"django.contrib.redirects.models.Redirect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Redirect</span></code></a> is not found for the\nrequested path or has a blank <code class=\"docutils literal notranslate\"><span class=\"pre\">new_path</span></code> value.</p>\n<p>Defaults to <a class=\"reference internal\" href=\"/en/6.1/ref/request-response/#django.http.HttpResponseGone\" title=\"django.http.HttpResponseGone\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponseGone</span></code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class\">\n<span class=\"sig-name descname\"><span class=\"pre\">response_redirect_class</span></span><a class=\"heading-anchor\" href=\"#django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <a class=\"reference internal\" href=\"/en/6.1/ref/request-response/#django.http.HttpResponse\" title=\"django.http.HttpResponse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponse</span></code></a> class that handles the redirect.</p>\n<p>Defaults to <a class=\"reference internal\" href=\"/en/6.1/ref/request-response/#django.http.HttpResponsePermanentRedirect\" title=\"django.http.HttpResponsePermanentRedirect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">HttpResponsePermanentRedirect</span></code></a>.</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>","rootId":"module-django.contrib.redirects","toc":[{"title":"Installation","anchor":"installation","children":[]},{"title":"How it works","anchor":"how-it-works","children":[]},{"title":"How to add, change and delete redirects","anchor":"how-to-add-change-and-delete-redirects","children":[{"title":"Via the admin interface","anchor":"via-the-admin-interface","children":[]},{"title":"Via the Python API","anchor":"via-the-python-api","children":[]}]},{"title":"Middleware","anchor":"middleware","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/en/6.1/ref/"},{"docname":"ref/contrib/index","title":"contrib packages","url":"/en/6.1/ref/contrib/"}],"prev":{"docname":"ref/contrib/postgres/validators","title":"Validators","url":"/en/6.1/ref/contrib/postgres/validators/"},"next":{"docname":"ref/contrib/sitemaps","title":"The sitemap framework","url":"/en/6.1/ref/contrib/sitemaps/"},"formats":{"html":"/en/6.1/ref/contrib/redirects/","markdown":"/en/6.1/ref/contrib/redirects.md","json":"/en/6.1/ref/contrib/redirects.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/ref/contrib/redirects.txt","official":"https://docs.djangoproject.com/en/6.1/ref/contrib/redirects/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}