Module

postprocess.redirects

Redirect page generation for page aliases.

Generates redirect HTML pages for each page alias, allowing old URLs to redirect to new canonical locations. This preserves SEO and maintains link stability during content reorganization.

Usage:

from bengal.postprocess.redirects import RedirectGenerator

redirect_gen = RedirectGenerator(site)
redirect_gen.generate()

Classes

RedirectGenerator
Generates redirect HTML pages for page aliases. For each page with aliases, creates lightweight HT…
5

Generates redirect HTML pages for page aliases.

For each page with aliases, creates lightweight HTML files at the alias paths that redirect to the canonical URL. Includes proper SEO signals (canonical link, noindex, meta refresh).

Optionally generates a _redirects file for Netlify/Vercel for faster server-side redirects.

Methods 1

generate
Generate all redirect pages. Iterates through all pages, generates redirect HT…
0 int
def generate(self) -> int

Generate all redirect pages.

Iterates through all pages, generates redirect HTML for each alias, and optionally generates a _redirects file for platform-specific server-side redirects.

Returns

int

Number of redirects generated

Internal Methods 4
__init__
Initialize redirect generator.
1 None
def __init__(self, site: Site) -> None

Initialize redirect generator.

Parameters 1
site Site

Site instance with pages containing aliases

_generate_redirect
Generate a single redirect HTML page.
2 bool
def _generate_redirect(self, from_path: str, to_url: str) -> bool

Generate a single redirect HTML page.

Parameters 2
from_path str

Source path (alias), e.g., "/old/posts/my-post/"

to_url str

Target URL to redirect to, e.g., "/blog/my-post/"

Returns

bool

True if redirect was generated, False if skipped (conflict)

_render_redirect_html
Render redirect HTML with SEO hints. Generates a minimal HTML page that: - Red…
2 str
def _render_redirect_html(self, from_path: str, to_url: str) -> str

Render redirect HTML with SEO hints.

Generates a minimal HTML page that:

  • Redirects immediately via meta refresh
  • Includes canonical link for SEO
  • Has noindex to prevent redirect pages appearing in search
  • Provides fallback link for browsers without JavaScript/meta refresh
Parameters 2
from_path str

Original path (for display only)

to_url str

Target URL to redirect to

Returns

str

HTML string for redirect page

_generate_redirects_file
Generate _redirects file for Netlify/Vercel. Only generates if enabled in conf…
1 None
def _generate_redirects_file(self, alias_map: dict[str, list[tuple[str, str]]]) -> None

Generate _redirects file for Netlify/Vercel.

Only generates if enabled in config:

[redirects]
generate_redirects_file = true
Parameters 1
alias_map dict[str, list[tuple[str, str]]]

Map of alias -> [(target_url, title), ...]