Skip to content

Commit f9bd807

Browse files
committed
Sitemaps: Improve sitemap.xml redirects when using custom permalinks.
Changes the way redirects from `sitemap.xml` to `wp-sitemap.xml` happen, so that they also work when using a more complex custom rewrite structure. Props gmariani405, swissspidy, euthelup, peterwilsoncc. Fixes #61931. git-svn-id: https://develop.svn.wordpress.org/trunk@59228 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1dcbe10 commit f9bd807

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/wp-includes/class-wp-rewrite.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,9 @@ public function rewrite_rules() {
12871287
// favicon.ico -- only if installed at the root.
12881288
$favicon_rewrite = ( empty( $home_path['path'] ) || '/' === $home_path['path'] ) ? array( 'favicon\.ico$' => $this->index . '?favicon=1' ) : array();
12891289

1290+
// sitemap.xml -- only if installed at the root.
1291+
$sitemap_rewrite = ( empty( $home_path['path'] ) || '/' === $home_path['path'] ) ? array( 'sitemap\.xml' => $this->index . '??sitemap=index' ) : array();
1292+
12901293
// Old feed and service files.
12911294
$deprecated_files = array(
12921295
'.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this->index . '?feed=old',
@@ -1449,9 +1452,9 @@ public function rewrite_rules() {
14491452

14501453
// Put them together.
14511454
if ( $this->use_verbose_page_rules ) {
1452-
$this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules );
1455+
$this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $sitemap_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_rewrite, $post_rewrite, $this->extra_rules );
14531456
} else {
1454-
$this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules );
1457+
$this->rules = array_merge( $this->extra_rules_top, $robots_rewrite, $favicon_rewrite, $sitemap_rewrite, $deprecated_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules );
14551458
}
14561459

14571460
/**

src/wp-includes/sitemaps/class-wp-sitemaps.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public function init() {
7575
$this->register_sitemaps();
7676

7777
// Add additional action callbacks.
78-
add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml' ), 10, 2 );
7978
add_filter( 'robots_txt', array( $this, 'add_robots' ), 0, 2 );
8079
}
8180

@@ -223,6 +222,7 @@ public function render_sitemaps() {
223222
* Redirects a URL to the wp-sitemap.xml
224223
*
225224
* @since 5.5.0
225+
* @deprecated 6.7.0 Deprecated in favor of {@see WP_Rewrite::rewrite_rules()}
226226
*
227227
* @param bool $bypass Pass-through of the pre_handle_404 filter value.
228228
* @param WP_Query $query The WP_Query object.

tests/phpunit/tests/canonical/sitemaps.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public function test_sitemaps_canonical_pretty_redirects( $test_url, $expected )
5151
$this->assertCanonical( $test_url, $expected, 50910 );
5252
}
5353

54+
/**
55+
* Ensure sitemaps redirects work as expected with a more custom rewrite structure.
56+
*
57+
* @dataProvider data_sitemaps_canonical_pretty_redirects
58+
* @ticket 61931
59+
*/
60+
public function test_sitemaps_canonical_custom_pretty_redirects( $test_url, $expected ) {
61+
$this->set_permalink_structure( '/%category%/%year%/%monthnum%/%postname%/' );
62+
$this->assertCanonical( $test_url, $expected, 61931 );
63+
}
64+
5465
/**
5566
* Data provider for test_sitemaps_canonical_pretty_redirects.
5667
*
@@ -61,8 +72,12 @@ public function test_sitemaps_canonical_pretty_redirects( $test_url, $expected )
6172
* @type string $1 The expected canonical URL.
6273
* }
6374
*/
64-
public function data_sitemaps_canonical_pretty_redirects() {
75+
public static function data_sitemaps_canonical_pretty_redirects() {
6576
return array(
77+
// sitemap.xml special case.
78+
array( '/sitemap.xml', '/wp-sitemap.xml' ),
79+
array( '/sitemap.xml/', '/wp-sitemap.xml' ),
80+
6681
// Ugly/incorrect versions redirect correctly.
6782
array( '/?sitemap=index', '/wp-sitemap.xml' ),
6883
array( '/wp-sitemap.xml/', '/wp-sitemap.xml' ),

0 commit comments

Comments
 (0)