Canonical: Redirect when front page's paginated states not found.

Perform a canonical redirect for an invalid pagination request of a static front page.

When a site has a static front page assigned and that page has a `<!--nextpage-->` within its content, previously accessing non-existing pages (e.g. `example.com/page/3/`) did not redirect or return a 404 or 301. This changeset resolves that issue by performing a canonical redirect.

Unit tests are also included for this specific use case and to ensure the fix does not affect a blog listing home page.

Follow-up to [47738], [47727], [34492].

Props dd32, audrasjb, chaion07, hellofromTonya, joemcgill, lukecarbis, Mte90, mukesh27, peterwilsoncc, rajinsharwar, SergeyBiryukov. 
Fixes #50163.
See meta#5184.
Built from https://develop.svn.wordpress.org/trunk@59091


git-svn-id: http://core.svn.wordpress.org/trunk@58487 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2024-09-25 19:37:19 +00:00
parent 254251d7c6
commit 0f1eaa6ca3
2 changed files with 6 additions and 5 deletions

View File

@ -747,14 +747,15 @@ class WP {
$content_found = true; $content_found = true;
if ( is_singular() ) { if ( is_singular() ) {
$post = isset( $wp_query->post ) ? $wp_query->post : null; $post = isset( $wp_query->post ) ? $wp_query->post : null;
$next = '<!--nextpage-->'; $next = '<!--nextpage-->';
$page_qv = is_front_page() ? 'paged' : 'page';
// Check for paged content that exceeds the max number of pages. // Check for paged content that exceeds the max number of pages.
if ( $post && ! empty( $this->query_vars['page'] ) ) { if ( $post && ! empty( $this->query_vars[ $page_qv ] ) ) {
// Check if content is actually intended to be paged. // Check if content is actually intended to be paged.
if ( str_contains( $post->post_content, $next ) ) { if ( str_contains( $post->post_content, $next ) ) {
$page = trim( $this->query_vars['page'], '/' ); $page = trim( $this->query_vars[ $page_qv ], '/' );
$content_found = (int) $page <= ( substr_count( $post->post_content, $next ) + 1 ); $content_found = (int) $page <= ( substr_count( $post->post_content, $next ) + 1 );
} else { } else {
$content_found = false; $content_found = false;

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.7-alpha-59090'; $wp_version = '6.7-alpha-59091';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.