404 for pagination pages of individual posts

Post content can be split with the tag <!--nextpage-->, so each text part will be available at its own address.

For example, let’s add 2 such tags to the content, thereby splitting it into 3 parts, which will be accessible at the following addresses:

  • Part 1 — https://site.com/test-post/
  • Part 2 — https://site.com/test-post/2/
  • Part 3 — https://site.com/test-post/3/

If you go to https://site.com/test-post/1/, it will redirect to https://site.com/test-post/, everything is logical. But if you go to https://site.com/test-post/4/ or any other non-existent part of the content, then everything will be displayed as usual, but there will be no text (part 4 doesn’t exist). That means the page exists, but there is no content. In such cases an error 404 should be returned, but WP sends a 200 response, which may be bad for SEO. Let’s fix this behavior.

This drawback is not on all sites!

By default, WP redirects to the main URL with a non-existent pagination page of posts.

Most likely this is related to disabling the redirect_canonical() function:

remove_action( 'template_redirect', 'redirect_canonical' );
// Option 1. Disable pagination for individual pages
// (the trend is that no one uses them)
add_filter( 'pre_handle_404', 'remove_single_post_pagination', 10, 2 );

function remove_single_post_pagination( $false, $wp_query ) {

	if ( is_singular() && get_query_var( 'page' ) ) {

		$wp_query->set_404();
		status_header( 404 );
		nocache_headers();

		return 'stop';
	}

	return $false;
}

Note embeded into: pre_handle_404