Skip to main content

A flexible URL redirection system with admin interface and logging.

Project description

plain.redirection

A flexible URL redirection system with admin interface and logging.

Overview

plain.redirection provides a database-driven URL redirection system for Plain applications. It includes:

  • Database models for managing redirects
  • Middleware that intercepts 404s and checks for matching redirects
  • Support for both exact matches and regex patterns
  • Comprehensive logging of redirects and 404s
  • Built-in admin interface for managing redirects

Basic Usage

Setting up the middleware

Add the RedirectionMiddleware to your middleware stack in settings.py:

MIDDLEWARE = [
    # ... other middleware ...
    "plain.redirection.RedirectionMiddleware",
    # This should typically be near the end of the middleware stack
]

Creating redirects

You can create redirects programmatically using the Redirect model:

from plain.redirection.models import Redirect

# Simple path redirect
Redirect.objects.create(
    from_pattern="/old-page/",
    to_pattern="/new-page/",
    http_status=301  # Permanent redirect
)

# Redirect with different status code
Redirect.objects.create(
    from_pattern="/temporary-page/",
    to_pattern="/replacement-page/",
    http_status=302  # Temporary redirect
)

Advanced Features

Regex redirects

For more complex URL patterns, you can use regex redirects:

# Redirect all blog posts to a new URL structure
Redirect.objects.create(
    from_pattern=r"^/blog/(\d{4})/(\d{2})/(.+)/$",
    to_pattern=r"/posts/\1-\2-\3/",
    is_regex=True,
    http_status=301
)

Redirect ordering

When multiple redirects might match a URL, you can control which one takes precedence using the order field:

# This redirect will be checked first (lower order = higher priority)
Redirect.objects.create(
    from_pattern="/special-case/",
    to_pattern="/handled-specially/",
    order=10
)

# This more general redirect will be checked later
Redirect.objects.create(
    from_pattern=r"^/special-.*/$",
    to_pattern="/general-handler/",
    is_regex=True,
    order=20
)

Logging

The package automatically logs all redirects and 404s:

  • RedirectLog - Records successful redirects with request metadata
  • NotFoundLog - Records 404s that didn't match any redirect

Access logs programmatically:

from plain.redirection.models import RedirectLog, NotFoundLog

# Recent redirects
recent_redirects = RedirectLog.objects.all()[:10]

# 404s from a specific IP
not_founds = NotFoundLog.objects.filter(ip_address="192.168.1.1")

Admin Interface

The package includes admin views for managing redirects and viewing logs. Once installed, you'll find three new sections in your admin:

  • Redirects - Create, edit, and delete redirect rules
  • Redirect logs - View successful redirects with full request details
  • 404 logs - Monitor URLs that resulted in 404s

The admin interface is automatically registered and will appear in the "Redirection" section of your Plain admin.

Installation

Install the plain.redirection package from PyPI:

uv add plain.redirection

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plain_redirection-0.20.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plain_redirection-0.20.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file plain_redirection-0.20.0.tar.gz.

File metadata

  • Download URL: plain_redirection-0.20.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.12

File hashes

Hashes for plain_redirection-0.20.0.tar.gz
Algorithm Hash digest
SHA256 8e946b0aea01603f9e2e9480d2f94c05fbec7f47e22ab76ce0486763f596b9d9
MD5 eb5b0f2a7edc78dcc1f6765a3f913652
BLAKE2b-256 3505ef6af6f367400c149c3f44d6807de20780a8aa6ec8a587207013b8b2123c

See more details on using hashes here.

File details

Details for the file plain_redirection-0.20.0-py3-none-any.whl.

File metadata

File hashes

Hashes for plain_redirection-0.20.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7250efa9ba9c48f2f0a7b7def36fcb68443fb7e346d6605cc3cca61b7f246f3d
MD5 412d95ec268b496eadb1ff9086433655
BLAKE2b-256 36984758042e62abfa04ec50ef0115c9350d8a17ce3305fafbd53451b6b1c0b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page