---
canonical: "https://www.jsolly.com/blog/django-automatic-redirects-in-two-lines-of-code/"
title: "Fixing Broken Links With Django's Redirects App"
description: "Learn how to use the redirects app in Django to redirect old URLs to new ones, avoiding 404 errors | A guide"
author: "John Solly"
published: "2022-07-24T22:09:57.597Z"
updated: "2022-07-24T22:09:57.597Z"
---

<a id="fixing-broken-links-with-djangos-redirects-app"></a>

# Fixing Broken Links With Django's Redirects App

![Lead actor of Django Unchained pointing a gun at the camera](https://d1d7p8ufhgz4ld.cloudfront.net/media/post_metaimgs/django.webp)

Let's say you make a post about your favorite color.

`/red-is-my-favorite-color`

Time passes, and you decide red isn't so hot anymore. Now your favorite color is blue. So you change the slug.

`/blue-is-my-favorite-color`

But now everyone who visits the original link gets a 404. To fix that up, I followed the [Django Docs](https://docs.djangoproject.com/en/4.0/ref/contrib/redirects/) and added the redirects app to my Django project in [this commit](https://github.com/jsolly/blogthedata/commit/2524fb88d9f56ef5b80a868041149428b12760bc). as of July 2022, all you have to do is:

1.  Ensure that the django.contrib.sites framework is installed.
2.  Add 'django.contrib.redirects' to your INSTALLED\_APPS setting.
3.  Add 'django.contrib.redirects.middleware.RedirectFallbackMiddleware' to your MIDDLEWARE setting.
4.  Run the command manage.py migrate.

Now on your admin page, you can add all the redirects you want

![screenshot of redirect page showing /red-is-my-favorite-color redirecting to /blue-is-my-favorite-color](https://d1d7p8ufhgz4ld.cloudfront.net/media/post_imgs/image_qzOXJUh.png)

I could add something in signals.py to do this automatically on slug change, but I don't want this behavior to be automagic, and I don't change link slugs very often.

Hope that was useful to you 😁

July 24, 2022 in [Web Dev](https://www.jsolly.com/blog/category/web-dev/)

Updated July 24, 2022
