close

Making WordPress.org

Changeset 14700


Ignore:
Timestamp:
03/12/2026 03:30:31 AM (41 hours ago)
Author:
dd32
Message:

Plugin Directory: Fix 404 redirect path parsing to use home_url prefix.

Instead of hardcoded array indices assuming a fixed URL structure, strip the home_url path prefix to get relative path segments.
This makes 404 redirects work correctly in non-production environments.

See https://github.com/WordPress/wordpress.org/pull/555

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r14667 r14700  
    14461446        if ( is_404() ) {
    14471447
    1448             // [1] => plugins [2] => example-plugin-name [3..] => random().
    1449             $path = explode( '/', trailingslashit( explode( '?', $_SERVER['REQUEST_URI'] )[0] ) );
    1450 
    1451             if ( 'tags' === $path[2] ) {
    1452                 if ( isset( $path[3] ) && ! empty( $path[3] ) ) {
    1453                     wp_safe_redirect( home_url( '/search/' . urlencode( $path[3] ) . '/' ), 301 );
     1448            $path_prefix = wp_parse_url( home_url('/'), PHP_URL_PATH );
     1449            $path        = substr( $_SERVER['REQUEST_URI'], strlen( $path_prefix ) );
     1450            // [0] => example-plugin-name [1..] => random().
     1451            $path        = explode( '/', trailingslashit( explode( '?', $path )[0] ) );
     1452            $path_base   = $path[0];
     1453
     1454            if ( 'tags' === $path_base ) {
     1455                if ( isset( $path[1] ) && ! empty( $path[1] ) ) {
     1456                    wp_safe_redirect( home_url( '/search/' . urlencode( $path[1] ) . '/' ), 301 );
    14541457                    die();
    14551458                } else {
     
    14601463
    14611464            // The about page is now over at /developers/.
    1462             if ( 'about' === $path[2] ) {
    1463                 if ( isset( $path[3] ) && 'add' == $path[3] ) {
     1465            if ( 'about' === $path_base ) {
     1466                if ( isset( $path[1] ) && 'add' == $path[1] ) {
    14641467                    wp_safe_redirect( home_url( '/developers/add/' ), 301 );
    1465                 } elseif ( isset( $path[3] ) && 'validator' == $path[3] ) {
     1468                } elseif ( isset( $path[1] ) && 'validator' == $path[1] ) {
    14661469                    wp_safe_redirect( home_url( '/developers/readme-validator/' ), 301 );
    14671470                } else {
     
    14721475
    14731476            // Browse 404s.
    1474             if ( 'browse' === $path[2] ) {
     1477            if ( 'browse' === $path_base ) {
    14751478                wp_safe_redirect( home_url( '/' ), 301 );
    14761479                die();
     
    14781481
    14791482            // The readme.txt page.
    1480             if ( 'readme.txt' === $path[2] ) {
     1483            if ( 'readme.txt' === $path_base ) {
    14811484                status_header( 200 );
    14821485                header( 'Content-type: text/plain' );
     
    14861489
    14871490            // Handle any plugin redirects.
    1488             if ( $path[2] && ( $plugin = self::get_plugin_post( $path[2] ) ) ) {
     1491            if ( $path_base && ( $plugin = self::get_plugin_post( $path_base ) ) ) {
    14891492                $permalink = get_permalink( $plugin->ID );
    14901493                if ( parse_url( $permalink, PHP_URL_PATH ) != $_SERVER['REQUEST_URI'] ) {
     
    14951498
    14961499            // Otherwise, let's redirect to the search page.
    1497             if ( isset( $path[2] ) && ! empty( $path[2] ) ) {
    1498                 wp_safe_redirect( home_url( '/search/' . urlencode( $path[2] ) . '/' ), 301 );
     1500            if ( isset( $path_base ) && ! empty( $path_base ) ) {
     1501                wp_safe_redirect( home_url( '/search/' . urlencode( $path_base ) . '/' ), 301 );
    14991502                die();
    15001503            }
Note: See TracChangeset for help on using the changeset viewer.