From 12f4c30551505a88689a7578a52225b2662294a1 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 4 Dec 2015 23:10:09 +0000 Subject: [PATCH] Canonical: introduce `strip_fragment_from_url()` and use when comparing URLs in `redirect_canonical()`. Props tellyworth. Fixes #19918. git-svn-id: https://develop.svn.wordpress.org/trunk@35770 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/canonical.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index 84b90e279e..cacbdaadfe 100644 --- a/src/wp-includes/canonical.php +++ b/src/wp-includes/canonical.php @@ -489,7 +489,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url ); // yes, again -- in case the filter aborted the request - if ( ! $redirect_url || $redirect_url == $requested_url ) { + if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) { return; } @@ -534,6 +534,31 @@ function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $ur return $query_string; } +/** + * Strips the #fragment from a URL, if one is present. + * + * @since 4.4.0 + * + * @param string $url The URL to strip. + * @return string The altered URL. + */ +function strip_fragment_from_url( $url ) { + $parsed_url = @parse_url( $url ); + if ( ! empty( $parsed_url['host'] ) ) { + // This mirrors code in redirect_canonical(). It does not handle every case. + $url = $parsed_url['scheme'] . '://' . $parsed_url['host']; + if ( ! empty( $parsed_url['port'] ) ) { + $url .= ':' . $parsed_url['port']; + } + $url .= $parsed_url['path']; + if ( ! empty( $parsed_url['query'] ) ) { + $url .= '?' . $parsed_url['query']; + } + } + + return $url; +} + /** * Attempts to guess the correct URL based on query vars *