---
title: "Aplikasi pengalihan"
version: 1.9
locale: id
source: https://docs.djangoproject.com/id/1.9/ref/contrib/redirects/
canonical: https://djangodocs.dev/id/1.9/ref/contrib/redirects/
---
# Aplikasi pengalihan

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.

## Pemasangan

Untuk memasang aplikasi pengalihan, ikuti langkah-langkah ini:

1. Ensure that the `django.contrib.sites` framework
   [is installed](/id/1.9/ref/contrib/sites/#enabling-the-sites-framework).
2. Add `'django.contrib.redirects'` to your [`INSTALLED_APPS`](/id/1.9/ref/settings/#std-setting-INSTALLED_APPS) setting.
3. Add `'django.contrib.redirects.middleware.RedirectFallbackMiddleware'`
   to your [`MIDDLEWARE_CLASSES`](/id/1.9/ref/settings/#std-setting-MIDDLEWARE_CLASSES) setting.
4. Jalankan perintah [`manage.py migrate`](/id/1.9/ref/django-admin/#django-admin-migrate).

## Bagaimana itu bekerja

`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 [`RedirectFallbackMiddleware`](#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
[`SITE_ID`](/id/1.9/ref/settings/#std-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
  [`RedirectFallbackMiddleware`](#django.contrib.redirects.middleware.RedirectFallbackMiddleware)
  and set
  [`response_redirect_class`](#django.contrib.redirects.middleware.RedirectFallbackMiddleware.response_redirect_class)
  to [`django.http.HttpResponseRedirect`](/id/1.9/ref/request-response/#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
  usual.

The middleware only gets activated for 404s -- not for 500s or responses of any
other status code.

Note that the order of [`MIDDLEWARE_CLASSES`](/id/1.9/ref/settings/#std-setting-MIDDLEWARE_CLASSES) matters. Generally, you
can put [`RedirectFallbackMiddleware`](#django.contrib.redirects.middleware.RedirectFallbackMiddleware)
at the end of the list, because it's a last resort.

Untuk lebih pada middleware, baca [dokumentasi middleware](/id/1.9/topics/http/middleware/).

## Bagaimana menambah, merubah dan menghapus pengalihan

### Melalui antarmuka admin

If you've activated the automatic Django admin interface, you should see a
"Redirects" section on the admin index page. Edit redirects as you edit any
other object in the system.

### Melalui API Python

#### `class models.Redirect`

Redirects are represented by a standard [Django model](/id/1.9/topics/db/models/),
which lives in [django/contrib/redirects/models.py](https://github.com/django/django/blob/master/django/contrib/redirects/models.py). You can access redirect
objects via the [Django database API](/id/1.9/topics/db/queries/).

## Middleware

#### `class middleware.RedirectFallbackMiddleware`

You can change the [`HttpResponse`](/id/1.9/ref/request-response/#django.http.HttpResponse) classes used
by the middleware by creating a subclass of
[`RedirectFallbackMiddleware`](#django.contrib.redirects.middleware.RedirectFallbackMiddleware)
and overriding `response_gone_class` and/or `response_redirect_class`.

#### `response_gone_class`

The [`HttpResponse`](/id/1.9/ref/request-response/#django.http.HttpResponse) class used when a
[`Redirect`](#django.contrib.redirects.models.Redirect) is not found for the
requested path or has a blank `new_path` value.

Defaults to [`HttpResponseGone`](/id/1.9/ref/request-response/#django.http.HttpResponseGone).

#### `response_redirect_class`

Kelas [`HttpResponse`](/id/1.9/ref/request-response/#django.http.HttpResponse) yang mengangani pengalihan.

Defaults to [`HttpResponsePermanentRedirect`](/id/1.9/ref/request-response/#django.http.HttpResponsePermanentRedirect).
