WordPress.org

Make WordPress Core

Ticket #19918: 19918-circular-fragment.diff

File 19918-circular-fragment.diff, 1.3 KB (added by tellyworth, 4 years ago)

Fixes a potential circular redirect issue.

  • wp-includes/canonical.php

     
    494494        $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
    495495
    496496        // yes, again -- in case the filter aborted the request
    497         if ( ! $redirect_url || $redirect_url == $requested_url ) {
     497        if ( ! $redirect_url || strip_fragment_from_url($redirect_url) == strip_fragment_from_url($requested_url) ) {
    498498                return;
    499499        }
    500500
     
    540540}
    541541
    542542/**
     543 * Strips the #fragment from a URL, if one is present.
     544 *
     545 * @param string $url The URL to strip.
     546 * @return string The altered URL.
     547 */
     548function strip_fragment_from_url( $url ) {
     549        $parsed_url = @parse_url( $url );
     550        if ( !empty( $parsed_url['host'] ) ) {
     551                // This mirrors code in redirect_canonical(). It does not handle every case.
     552                $url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
     553                if ( !empty($parsed_url['port']) )
     554                        $url .= ':' . $parsed_url['port'];
     555                $redirect_url .= $parsed_url['path'];
     556                if ( !empty($parsed_url['query']) )
     557                        $url .= '?' . $parsed_url['query'];
     558        }
     559
     560        return $url;
     561}
     562
     563/**
    543564 * Attempts to guess the correct URL based on query vars
    544565 *
    545566 * @since 2.3.0