Skip to content

Conversation

oopen
Copy link

@oopen oopen commented Oct 8, 2025

📝 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:

  • The old URL becomes a 404.
  • BookStack does not store historical slugs for books or chapters (only pages have revisions via page_revisions).
  • External links, bookmarks, or search engine results pointing to the old URL break permanently.

This creates a poor user experience and harms SEO.

✅ Solution

The new middleware:

  • Runs early in the web middleware stack (after TrustProxies, before PageViewCounter).
  • Extracts the last path segment from the request (e.g., /book/foo/barbar).
  • Checks if this segment exists as a non-current slug in page_revisions.
  • If found, issues a 301 redirect to /link/{page_id} — BookStack’s built-in, stable permalink route.
  • Skips API, asset, debug, and system routes to avoid interference.

This approach is:

  • 100% reliable for pages (uses real revision data).
  • Independent of book/chapter slug history (which BookStack doesn’t store).
  • Zero-config and fully automatic.
  • Safe: only redirects when an exact match is found in revisions.

🧪 Testing

  1. Create a page → note its slug.
  2. Edit the page and change its slug.
  3. Visit any URL ending with the old slug (e.g., /any/book/or/chapter/path/old-slug).
  4. ✅ Observe a 301 redirect to /link/{id}, then to the current page.

Note: The middleware matches any path ending with the old slug, because BookStack’s routing is dynamic and doesn’t enforce strict path structures. This ensures maximum compatibility.

📁 Files Changed

  • app/Http/Middleware/RedirectOldPageSlugs.php → new middleware
  • app/Http/Kernel.php → register middleware in web group

⚙️ Middleware Placement

Inserted after TrustProxies and before PageViewCounter to:

  • Avoid counting redirected requests as page views.
  • Ensure trusted proxy headers are processed first.

💡 Why /link/{id} and not the canonical URL?

  • /link/{id} is BookStack’s official, stable permalink mechanism.
  • It already handles final redirection to the current URL (including updated book/chapter slugs).
  • This avoids duplicating routing logic and ensures consistency.

✅ Benefits

  • Restores broken links automatically.
  • Improves SEO by preserving link equity via 301s.
  • Requires no user intervention or configuration.
  • Minimal performance impact (indexed DB lookup on 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!

oopen added 2 commits October 8, 2025 10:48
Restores broken links automatically.
Improves SEO by preserving link equity via 301s.
Requires no user intervention or configuration.
Minimal performance impact (indexed DB lookup on page_revisions.slug).
@oopen oopen changed the title feat(middleware): Restore all 404 old links, caused when page title (slug) change, add dynamic redirect for old page slugs using page revisions feat(middleware): Page 404 to 301 | add redirect for old page slugs, caused by title change Oct 8, 2025
@ssddanbrown
Copy link
Member

Thanks for offering this @oopen, but this isn't the approach I'd like to take, and as mentioned in #5827 I'm awaiting on other changes before adding this kind of functionality.

Also, please avoid writing PRs (text/code) with heavy AI use for this project.
It adds a lot of extra incorrect information, and code that does not understand the full context which can result in issues or odd behaviour/decisions.

@oopen
Copy link
Author

oopen commented Oct 11, 2025

Ok, I read your plan to update the DB first, it a good thing.

But please consider this redirection feature as a "temporary mitigation hack",
and can be removed after when you complete the plan you described #5800

It make no change to the app/DB, just fix a big part of 404 links immediately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants