Skip to content

Commit

Permalink
Canonical: Move trailing slash handling for robots.txt and sitemaps…
Browse files Browse the repository at this point in the history
… to a single condition.

Give the unit test a more descriptive name.

Follow-up to [48153], [48155].

See #48025.

git-svn-id: https://develop.svn.wordpress.org/trunk@48166 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Jun 25, 2020
1 parent 72c7f2c commit de36eb3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
15 changes: 6 additions & 9 deletions src/wp-includes/canonical.php
Expand Up @@ -57,8 +57,8 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
}
}

if ( is_admin() || is_search() || is_preview() || is_trackback()
|| is_favicon() || ( $is_IIS && ! iis7_supports_permalinks() )
if ( is_admin() || is_search() || is_preview() || is_trackback() || is_favicon()
|| ( $is_IIS && ! iis7_supports_permalinks() )
) {
return;
}
Expand Down Expand Up @@ -655,8 +655,10 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
$redirect['path'] = trailingslashit( $redirect['path'] );
}

// Remove trailing slash for sitemaps requests.
if ( ! empty( get_query_var( 'sitemap' ) ) || ! empty( get_query_var( 'sitemap-stylesheet' ) ) ) {
// Remove trailing slash for robots.txt or sitemap requests.
if ( is_robots()
|| ! empty( get_query_var( 'sitemap' ) ) || ! empty( get_query_var( 'sitemap-stylesheet' ) )
) {
$redirect['path'] = untrailingslashit( $redirect['path'] );
}

Expand All @@ -682,11 +684,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
$redirect['host'] = $original['host'];
}

// Even if the permalink structure ends with a slash, remove slash robots.txt.
if ( is_robots() ) {
$redirect['path'] = untrailingslashit( $redirect['path'] );
}

$compare_original = array( $original['host'], $original['path'] );

if ( ! empty( $original['port'] ) ) {
Expand Down
8 changes: 2 additions & 6 deletions tests/phpunit/tests/canonical/robots.php
Expand Up @@ -7,12 +7,8 @@
*/
class Tests_Canonical_Robots extends WP_Canonical_UnitTestCase {

function setUp() {
parent::setUp();
}

function test_robots_url() {
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
public function test_remove_trailing_slashes_for_robots_requests() {
$this->set_permalink_structure( '/%postname%/' );
$this->assertCanonical( '/robots.txt', '/robots.txt' );
$this->assertCanonical( '/robots.txt/', '/robots.txt' );
}
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/canonical/sitemaps.php
Expand Up @@ -7,6 +7,7 @@
* @group sitemaps
*/
class Tests_Canonical_Sitemaps extends WP_Canonical_UnitTestCase {

public function setUp() {
parent::setUp();
$wp_sitemaps = new WP_Sitemaps();
Expand Down Expand Up @@ -38,4 +39,5 @@ public function test_remove_trailing_slashes_for_sitemap_stylesheet_requests() {
$this->assertCanonical( '/wp-sitemap.xsl', '/wp-sitemap.xsl' );
$this->assertCanonical( '/wp-sitemap.xsl/', '/wp-sitemap.xsl' );
}

}

0 comments on commit de36eb3

Please sign in to comment.