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_redirecttable as the rest of the CMS data. - Stale
Redirects Report - 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_pathtemplate: its single*(if any) becomescapture; without a*the capture is dropped (the whole matched subtree funnels to one URL). - bump_
hit_ count - Increment
hit_countfor 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_idviafrom_page_idorto_page_id, recordingreason. 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_redirecttable. Idempotent (duplicate-column errors are swallowed); called per tenant at boot fromcrate::seed::ensure_seeded— the same mechanism the seed-ensured tables use, since repo-widegen_migrationis 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
pathin the current tenant. ReturnsOk(None)when no row matches. - flush_
all - Flush every tenant’s pending deltas immediately (e.g. graceful
shutdown). Best-effort.
poolsare the per-tenant pools to write through, matched to the accumulated slugs. - is_
wildcard - A
from_pathis a wildcard PATTERN (vs an exact path) when it carries the single*glob. - match_
wildcard - Match a wildcard
pattern(exactly one*) againstpath. 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_liverules 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(mirrorscrate::auto_menu::prefetch_cached): serves the rule set fromcacheunder 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 → tosend a visitor back into itself? - resolve_
destination - Resolve the rule’s destination at serve time: a
to_page_idwins (the page’s currenturl_path, so the rule follows moves and renames) withto_pathas 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
Redirectrow into the framework’sRedirectRuleshape sorustango::redirects::build_redirect_responsecan produce the 301/302. - wildcard_
rules - The active wildcard (
from_pathcontains*) 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.