main ← fix-over-range-page-redirect
opened 02:47PM - 21 May 26 UTC
Requesting `/t/slug/id?page=N` with a page number beyond the topic's last valid …page raised `Discourse::NotFound` and returned 404. This produced a long-running stream of "increase in 404 errors" reports in Google Search Console, because crawlers (and our own `sitemap_recent.xml`) hold cached page URLs that become stale when a topic shrinks — most often after deleted posts or topic splits.
The 404 also masked a latent bug for staff: the original check compared the requested page against `topic.posts_count`, a cached column that excludes whispers and ignores active filters (`username_filters`, `replies_to_post_number`, etc.). A staff user navigating to `?page=N` covering whisper posts could be 404'd even though the page contained content they were entitled to see.
Replace the 404 with a 301 redirect to the last valid page (or the unparameterized topic URL if the topic fits in one page), using `@topic_view.filtered_posts.count` — the count of posts visible to the current viewer — so the math respects whispers, deletions visible to staff, and active filters. The COUNT query is gated behind `page > 1` so the common case (no `?page=` param) pays nothing extra.
Negative pages still raise 404; only the over-range case becomes a redirect.
https://meta.discourse.org/t/96337