Make WordPress Core


Ignore:
Timestamp:
03/17/2026 01:33:03 AM (6 months ago)
Author:
peterwilsoncc
Message:

Permalinks: Follow rewrite rules for trailing slashes in paginate_links().

Modifies the default settings of paginate_links() to only include trailing slashes, eg example.org/page/2/, when the permalink settings include a trailing slash. When the permalink settings do not include a slash, neither do the generated links, eg example.org/page/2.

This prevents the generated links from hitting a URL that subsequently redirects when the permalink structure does not include a trailing slash.

Props adamsilverstein, ankitkumarshah, audrasjb, hmbashar, huzaifaalmesbah, joedolson, juanmaguitar, krupajnanda, mai21, ozgursar, peterwilsoncc, rahulsprajapati, sirlouen, welcher, westonruter.
Fixes #61393.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r61768 r62036  
    46704670        $current = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
    46714671
    4672         // Append the format placeholder to the base URL.
    4673         $pagenum_link = trailingslashit( $url_parts[0] ) . '%_%';
     4672        /*
     4673         * Ensures sites not using trailing slashes get links in the form
     4674         * `/page/2` rather than `/page/2/`. On these sites, linking to the
     4675         * URL with a trailing slash will result in a 301 redirect from the
     4676         * incorrect URL to the correctly formatted one. This presents an
     4677         * unnecessary performance hit.
     4678         */
     4679        if ( $wp_rewrite->using_permalinks() && ! $wp_rewrite->use_trailing_slashes ) {
     4680                $pagenum_link = untrailingslashit( $url_parts[0] );
     4681        } else {
     4682                $pagenum_link = trailingslashit( $url_parts[0] );
     4683        }
     4684        $pagenum_link .= '%_%';
    46744685
    46754686        // URL base depends on permalink settings.
    46764687        $format  = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
    46774688        $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
     4689        if ( $wp_rewrite->using_permalinks() && ! $wp_rewrite->use_trailing_slashes ) {
     4690                $format = '/' . ltrim( $format, '/' );
     4691        }
    46784692
    46794693        $defaults = array(
Note: See TracChangeset for help on using the changeset viewer.