WordPress.org

Make WordPress Core

Changeset 6085


Ignore:
Timestamp:
09/11/2007 09:21:40 PM (11 years ago)
Author:
markjaquith
Message:

Always trailingslash the "home" URL. Protect against chained redirects. Thanks wantmoore. fixes #4773

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/canonical.php

    r6026 r6085  
    22// Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" by Mark Jaquith
    33
    4 function redirect_canonical() {
     4function redirect_canonical($requested_url=NULL, $do_redirect=true) {
    55    global $wp_rewrite, $posts, $is_IIS;
    66
     
    88        return;
    99
    10     // build the URL in the address bar
    11     $requested_url  = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
    12     $requested_url .= $_SERVER['HTTP_HOST'];
    13     $requested_url .= $_SERVER['REQUEST_URI'];
     10    if ( !$requested_url ) {
     11        // build the URL in the address bar
     12        $requested_url  = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
     13        $requested_url .= $_SERVER['HTTP_HOST'];
     14        $requested_url .= $_SERVER['REQUEST_URI'];
     15    }
    1416
    1517    $original = @parse_url($requested_url);
     
    127129    }
    128130
     131    // Always trailing slash the 'home' URL
     132    if ( $redirect['path'] == $user_home['path'] )
     133        $redirect['path'] = trailingslashit($redirect['path']);
     134
    129135    if ( array($original['host'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['path'], $redirect['query']) ) {
    130136        $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
     
    136142        // var_dump($redirect_url); die();
    137143        $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
    138         wp_redirect($redirect_url, 301);
    139         exit();
     144        if ( $do_redirect) {
     145            // protect against chained redirects
     146            if ( !redirect_canonical($redirect_url, false) ) {
     147                wp_redirect($redirect_url, 301);
     148                exit();
     149            } else {
     150                return false;
     151            }
     152        } else {
     153            return $redirect_url;
     154        }
     155    } else {
     156        return false;
    140157    }
    141158}
Note: See TracChangeset for help on using the changeset viewer.