Make WordPress Core


Ignore:
Timestamp:
09/10/2026 01:09:53 AM (2 months ago)
Author:
westonruter
Message:

Sitemaps: Don't 404 valid sitemaps on sites with no posts.

WP::handle_404() sets a 404 when the main query matches no posts and no exception applies. Sitemap requests were never among those exceptions; they were shielded only incidentally, by falling through to is_home, and in r62664 that fallthrough was removed. A site with no published posts therefore served a complete, valid sitemap under a 404 status, which search engines discard.

Exempt sitemap and stylesheet routes there, alongside the existing admin, robots and favicon exceptions. Since handle_404() no longer decides the status for these requests, every sitemap 404 now has to be issued by WP_Sitemaps::render_sitemaps() instead: an unregistered provider, an unrecognized stylesheet type, and a route whose query vars do not survive sanitize_text_field() would each otherwise be served as a 200 on an arbitrary URL. These share a send_404() helper, which also sends the no-cache headers handle_404() was previously contributing, so an intermediary does not retain a 404 for a route that becomes valid once the site has more content.

Sitemaps disabled via the wp_sitemaps_enabled filter, and providers with an empty URL list, keep the status they already had; whether the latter should render an empty sitemap instead is #61293.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/13247.
Follow-up to r48072, r48523, r62664.

Reviewed by adamsilverstein.
Merges r63570 to the 7.1 branch.
Merged to the 7.0 branch in error in r63573 and reverted in r63574.

Props iamchitti, westonruter, fernandot, wildworks, harishtewari, l1onofjudah, luksusspokoju, abrahamfariaz, andreasca, siliconforks, adamsilverstein, audrasjb, ocean90, mrkenobi.
See #39157, #61293.
Fixes #65945.

Location:
branches/7.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/7.1

  • branches/7.1/tests/phpunit/tests/sitemaps/sitemaps.php

    r57987 r63575  
    494494                $this->assertTrue( is_404() );
    495495        }
     496
     497        /**
     498         * Ensures a paged subtype route with no URLs still 404s, now that
     499         * WP::handle_404() no longer sets a 404 for sitemap requests.
     500         *
     501         * @ticket 65945
     502         */
     503        public function test_empty_url_list_for_subtype_should_return_404() {
     504                wp_register_sitemap_provider( 'foo', new WP_Sitemaps_Empty_Test_Provider( 'foo' ) );
     505
     506                $this->go_to( home_url( '/?sitemap=foo&sitemap-subtype=bar&paged=2' ) );
     507
     508                wp_sitemaps_get_server()->render_sitemaps();
     509
     510                $this->assertTrue( is_404() );
     511        }
     512
     513        /**
     514         * Ensures a sitemap query var that does not survive sanitizing 404s.
     515         *
     516         * WP::handle_404() exempts these requests on the raw query var, while
     517         * render_sitemaps() acts on the sanitized value. Without a matching bail
     518         * they fall through both and an arbitrary URL is served as a 200.
     519         *
     520         * @ticket 65945
     521         *
     522         * @dataProvider data_unusable_sitemap_query_vars
     523         *
     524         * @param non-falsy-string $query_string Query string to append to a nonexistent URL.
     525         */
     526        public function test_unusable_sitemap_query_var_should_return_404( string $query_string ) {
     527                $this->set_permalink_structure( '/%postname%/' );
     528
     529                // Instantiate the server before navigating: registering the sitemap
     530                // rewrite tags is what adds the query vars to `$wp->public_query_vars`.
     531                $sitemaps = wp_sitemaps_get_server();
     532
     533                $this->go_to( home_url( '/this-page-does-not-exist/' . $query_string ) );
     534
     535                $this->assertFalse( is_404(), 'WP::handle_404() should not have set a 404.' );
     536
     537                $sitemaps->render_sitemaps();
     538
     539                $this->assertTrue( is_404(), 'render_sitemaps() should have set a 404.' );
     540        }
     541
     542        /**
     543         * Data provider.
     544         *
     545         * @return array<non-falsy-string, array{ non-falsy-string }>
     546         */
     547        public function data_unusable_sitemap_query_vars(): array {
     548                return array(
     549                        'value stripped by sanitizing' => array( '?sitemap=<>' ),
     550                        'array sitemap value'          => array( '?sitemap[]=index' ),
     551                        'array stylesheet value'       => array( '?sitemap-stylesheet[]=sitemap' ),
     552                );
     553        }
     554
     555        /**
     556         * Ensures an unrecognized stylesheet type 404s from render_sitemaps().
     557         *
     558         * WP::handle_404() exempts any request carrying a `sitemap-stylesheet`
     559         * query var, and WP_Sitemaps_Stylesheet::render_stylesheet() echoes nothing
     560         * for a type other than 'sitemap' or 'index', so this route would otherwise
     561         * be served as a 200 with an empty body.
     562         *
     563         * @ticket 65945
     564         */
     565        public function test_unrecognized_stylesheet_type_should_return_404() {
     566                // Instantiate the server before navigating: registering the sitemap rewrite
     567                // tags is what adds `sitemap-stylesheet` to `$wp->public_query_vars`.
     568                $sitemaps = wp_sitemaps_get_server();
     569
     570                $this->go_to( home_url( '/?sitemap-stylesheet=this-is-not-a-stylesheet' ) );
     571
     572                $this->assertFalse( is_404(), 'WP::handle_404() should not have set a 404.' );
     573
     574                $sitemaps->render_sitemaps();
     575
     576                $this->assertTrue( is_404(), 'render_sitemaps() should have set a 404.' );
     577        }
     578
     579        /**
     580         * Ensures an unregistered provider 404s from render_sitemaps().
     581         *
     582         * WP::handle_404() exempts every sitemap request, so this route would
     583         * otherwise be served with a 200.
     584         *
     585         * @ticket 65945
     586         */
     587        public function test_unregistered_provider_should_return_404() {
     588                // Instantiate the server before navigating: registering the sitemap
     589                // rewrite tags is what adds `sitemap` to `$wp->public_query_vars`.
     590                $sitemaps = wp_sitemaps_get_server();
     591
     592                $this->go_to( home_url( '/?sitemap=this-provider-does-not-exist' ) );
     593
     594                $this->assertFalse( is_404(), 'WP::handle_404() should not have set a 404.' );
     595
     596                $sitemaps->render_sitemaps();
     597
     598                $this->assertTrue( is_404(), 'render_sitemaps() should have set a 404.' );
     599        }
    496600}
Note: See TracChangeset for help on using the changeset viewer.