feat(middleware): Page 404 to 301 | add redirect for old page slugs, caused by title change #5826
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📝 Description
This PR introduces a new Laravel middleware,
RedirectOldPageSlugs
, that automatically redirects requests containing obsolete page slugs to the page’s stable permalink (/link/{id}
), which then redirects to the current canonical URL.🔍 Problem
When a user changes a page’s slug in BookStack:
page_revisions
).This creates a poor user experience and harms SEO.
✅ Solution
The new middleware:
web
middleware stack (afterTrustProxies
, beforePageViewCounter
)./book/foo/bar
→bar
).page_revisions
./link/{page_id}
— BookStack’s built-in, stable permalink route.This approach is:
🧪 Testing
/any/book/or/chapter/path/old-slug
)./link/{id}
, then to the current page.📁 Files Changed
app/Http/Middleware/RedirectOldPageSlugs.php
→ new middlewareapp/Http/Kernel.php
→ register middleware inweb
group⚙️ Middleware Placement
Inserted after
TrustProxies
and beforePageViewCounter
to:💡 Why
/link/{id}
and not the canonical URL?/link/{id}
is BookStack’s official, stable permalink mechanism.✅ Benefits
page_revisions.slug
).🙏 Final Note
This feature aligns with BookStack’s philosophy of usability and data integrity, and leverages existing systems (
page_revisions
,/link/{id}
) without introducing redundancy.Thank you for considering this contribution!