WordPress.org

Making WordPress.org


Ignore:
Timestamp:
01/09/2019 12:40:52 AM (9 months ago)
Author:
dd32
Message:

Support Forums: Add a canonical redirect for .../page/[01]/ > .../ as these display the same content. This avoids duplicate content SEO issues.

Fixes #3944.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-performance-optimizations.php

    r7806 r8050  
    3333        // Redirect search results.
    3434        add_action( 'bbp_template_redirect', array( $this, 'redirect_search_results_to_google_search' ) );
     35
     36        // Redirect (.+)/page/[01]/ and (.+)?paged=[01] to $1
     37        add_action( 'bbp_template_redirect', array( $this, 'redirect_page_zero_one' ) );
    3538
    3639        // REST API.
     
    122125        wp_safe_redirect( $search_url );
    123126        exit;
     127    }
     128
     129    /**
     130     * Redirects /page/[01]/, and /?paged=[01] requests to the archive root.
     131     */
     132    public function redirect_page_zero_one() {
     133        global $wp_query;
     134
     135        if (
     136            isset( $wp_query->query['paged'] ) &&
     137            in_array( $wp_query->query['paged'], [ 0, 1 ] ) &&
     138            'POST' !== $_SERVER['REQUEST_METHOD']
     139        ) {
     140            // Generate the current URL.
     141            $current_url = $_SERVER['REQUEST_URI'];
     142            // Remove the path components.
     143            $current_url = preg_replace( '!^' . preg_quote( parse_url( home_url('/'), PHP_URL_PATH ), '!' ) . '!i', '', $current_url );
     144            $current_url = home_url( $current_url );
     145
     146            // Remove any paged items
     147            $pageless_url = $current_url;
     148            $pageless_url = remove_query_arg( 'paged', $pageless_url );
     149            $pageless_url = preg_replace( '!/page/[01]/?!i', '/', $pageless_url );
     150
     151            if ( $pageless_url !== $current_url ) {
     152                wp_safe_redirect( $pageless_url );
     153                exit;
     154            }
     155        }
    124156    }
    125157
Note: See TracChangeset for help on using the changeset viewer.