Skip to main content

Module redirect

Module redirect 

Source
Expand description

Editor-managed 301/302 redirects.

One row per redirect: from_path (the URL visitors hit) → to_path (the canonical destination). is_permanent = true emits 301 Moved Permanently; false emits 302 Found. The match is on the request path including trailing-slash variance — /old and /old/ are distinct rows so admins can pick the right shape for their case.

The public router consults the table whenever a slug lookup fails (crate::resolver::resolve_path returns None) — redirects hook into the 404 path. Live page URLs take precedence — a redirect from /about is ignored if a published page at /about exists.

Per-request rendering uses rustango::redirects::build_redirect_response so the Location header preserves any query string the visitor arrived with (/old?ref=ad → /new?ref=ad).

§Migration

Re-run cargo run --example cms_demo -- makemigrations after pulling this module so the framework generates the JSON file for cms_redirect. The Model derive registers the table via inventory; without the migration file the runtime DB schema stays unaware of the new table.

Structs§

Redirect
One redirect entry. Tenant-scoped — every row lives in the same per-tenant cms_redirect table as the rest of the CMS data.
StaleRedirectsReport
Registry report surfacing which redirects are worth retiring: never-used, long-unused, or auto-disabled. Appears in the Reports nav automatically via crate::register_report!.

Constants§

STALE_AFTER_DAYS
A rule with no hit for this many days counts as stale; a rule never hit since (roughly) creation counts as unused. Editors use the report to prune rules nothing points at anymore.

Functions§

apply_capture
Substitute a capture into a to_path template: its single * (if any) becomes capture; without a * the capture is dropped (the whole matched subtree funnels to one URL).
bump_hit_count
Increment hit_count for a single redirect row. Failures are logged + swallowed — analytics counters shouldn’t break the user-facing redirect.
disable_for_page
Disable every active rule that references page_id via from_page_id or to_page_id, recording reason. Called from the admin’s page delete / unpublish paths so a rule never silently serves a dead source or points at a vanished destination. Returns how many rules were disabled.
ensure_columns
Add the page-link columns to a pre-existing cms_redirect table. Idempotent (duplicate-column errors are swallowed); called per tenant at boot from crate::seed::ensure_seeded — the same mechanism the seed-ensured tables use, since repo-wide gen_migration is blocked. Fresh databases get the whole table, these columns included, from the JSON migrations; this upgrades a table created before them.
find_for_path
Look up a redirect for path in the current tenant. Returns Ok(None) when no row matches.
flush_all
Flush every tenant’s pending deltas immediately (e.g. graceful shutdown). Best-effort. pools are the per-tenant pools to write through, matched to the accumulated slugs.
is_wildcard
A from_path is a wildcard PATTERN (vs an exact path) when it carries the single * glob.
match_wildcard
Match a wildcard pattern (exactly one *) against path. The pattern splits into a prefix + suffix around the star; a path matches when it starts with the prefix, ends with the suffix, and is long enough to leave a (possibly empty) capture between them. Returns the captured segment (*’s expansion) on a match.
override_rules
The active overrides_live rules for this tenant. Consulted by the public router BEFORE page resolution, so it must stay cheap: the set is tiny in practice (usually empty) and the cached variant below amortizes it to one query per TTL.
override_rules_cached
Cached variant of override_rules (mirrors crate::auto_menu::prefetch_cached): serves the rule set from cache under a per-tenant key, recomputing on miss. Cache failures degrade to a live query; query failures degrade to an empty set (the router then just resolves pages normally).
redirect_loops
Would a rule from → to send a visitor back into itself?
resolve_destination
Resolve the rule’s destination at serve time: a to_page_id wins (the page’s current url_path, so the rule follows moves and renames) with to_path as the fallback when the page is missing or not served — the resolver’s rule, so an archived target is still followed.
to_rule
Convert a Redirect row into the framework’s RedirectRule shape so rustango::redirects::build_redirect_response can produce the 301/302.
wildcard_rules
The active wildcard (from_path contains *) rules for this tenant. Used on the exact-miss fallthrough.
wildcard_rules_cached
Cached variant of wildcard_rules — same short-TTL fragment cache as the override set, so tenants with no wildcard rules pay nothing per request.