<commit_before>=================
The redirects app
=================

.. module:: django.contrib.redirects
   :synopsis: A framework for managing redirects.

Django comes with an optional redirects application. It lets you store simple
redirects in a database and handles the redirecting for you.

Installation
============

To install the redirects app, follow these steps:

1. Ensure that the ``django.contrib.sites`` framework
   :ref:`is installed <enabling-the-sites-framework>`.
2. Add ``'django.contrib.redirects'`` to your :setting:`INSTALLED_APPS` setting.
3. Add ``'django.contrib.redirects.middleware.RedirectFallbackMiddleware'``
   to your :setting:`MIDDLEWARE_CLASSES` setting.
4. Run the command :djadmin:`manage.py migrate <migrate>`.

How it works
============

``manage.py migrate`` creates a ``django_redirect`` table in your database. This
is a simple lookup table with ``site_id``, ``old_path`` and ``new_path`` fields.

The :class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
does all of the work. Each time any Django 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
:setting:`SITE_ID` setting.

* If it finds a match, and ``new_path`` is not empty, it redirects to
  ``new_path``.
* 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 as<commit_msg>Fixed #21354 -- Documented contrib.redirects defaults to 301 redirects<commit_after>=================
The redirects app
=================

.. module:: django.contrib.redirects
   :synopsis: A framework for managing redirects.

Django comes with an optional redirects application. It lets you store simple
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:

1. Ensure that the ``django.contrib.sites`` framework
   :ref:`is installed <enabling-the-sites-framework>`.
2. Add ``'django.contrib.redirects'`` to your :setting:`INSTALLED_APPS` setting.
3. Add ``'django.contrib.redirects.middleware.RedirectFallbackMiddleware'``
   to your :setting:`MIDDLEWARE_CLASSES` setting.
4. Run the command :djadmin:`manage.py migrate <migrate>`.

How it works
============

``manage.py migrate`` creates a ``django_redirect`` table in your database. This
is a simple lookup table with ``site_id``, ``old_path`` and ``new_path`` fields.

The :class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
does all of the work. Each time any Django 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
:setting:`SITE_ID` setting.

* If it finds a match, and ``new_path`` is not empty, it redirects to
  ``new_path`` using a 301 ("Moved Permanently") redirect. You can subclass
  :class:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware`
  and set
  :attr:`~django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class`
  to :class:`django.http.HttpResponseRedirect` to use a
  ``302 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 as
{'fraction_non_alphanumeric': 0.12330623306233063, 'fraction_numerical': 0.01002710027100271, 'mean_word_length': 5.213804713804714, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7124845', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1024, 'n_words': 429}