Make WordPress Core

Changes between Version 3 and Version 4 of Ticket #63522


Ignore:
Timestamp:
06/04/2025 03:07:54 AM (8 months ago)
Author:
dd32
Comment:

Added an extra bonus in redirect_canonical() https://github.com/dd32/wordpress-develop/commit/648ad2a505e1817c9424800feacc06b0801fe630

redirect_canonical(): Don't attempt a ?page to /page/ redirect if the base url is unknown. When redirect_canonical() attempts to redirect from ?page=2 to /page/2/ if the base-url is unknown the result is an attempt to redirect to /2/ which through a series of unfortunate assumptions in the code (Only full URLs are expected) results in an attempt to redirect to ://hostname.example/2/ and causes a series of php warnings such as:

Warning: Undefined array key "scheme" in wp-includes/canonical.php on line 752
Warning: Undefined array key "host" in wp-includes/canonical.php on line 717
Warning: Undefined array key "host" in wp-includes/canonical.php on line 728
Warning: Undefined array key "host" in wp-includes/canonical.php on line 731

By only attempting to perform the singular pagination redirects when we actually have a singular permalink to redirect to, it prevents a chain of awkward warnings that appear unrelated.

Unfortunately I couldn't immediately figure out how to trigger it on a vanilla install, but here's the type of request from GlotPress:

GET https://translate.wordpress.test/projects/meta/wordpress-org/en-au/default/?name=blahblahblah&page=2

which eventually attempts:

redirect_canonical( $requested_url = '://translate.wordpress.test/2/?name=blahblahblah', $do_redirect = FALSE )

As such, there's an alternate way that could be avoided, and may work better in some situations, but the above change resolves the main warning.

  • wp-includes/canonical.php

     
    615618                unset( $redirect['port'] );
    616619        }
    617620
     621        // If the redirect scheme is unknown, default to the current scheme.
     622        if ( empty( $redirect['scheme'] ) ) {
     623                $redirect['scheme'] = is_ssl() ? 'https' : 'http';
     624        }
     625
    618626        // Trailing /index.php.
    619627        $redirect['path'] = preg_replace( '|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path'] );

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #63522 – Description

    v3 v4  
    1313(although in this case, it's not get_queried_object(), just an expectation of `get_post(0)` working, which is effectively the same thing)
    1414
     15and finally, in `redirect_canonical()`, when the permalink to a post can't be found:
     16> Warning: Undefined array key "scheme" in wp-includes/canonical.php on line 752
     17> Warning: Undefined array key "host" in wp-includes/canonical.php on line xxx