WordPress.org

Make WordPress Core

Changeset 5978


Ignore:
Timestamp:
08/29/2007 09:06:51 PM (11 years ago)
Author:
markjaquith
Message:

More cautious canonical-ness. Exclude IIS and POSTs. Better paging handling. Still a work in progress! see #4773

File:
1 edited

Legend:

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

    r5974 r5978  
    33
    44function redirect_canonical() {
    5     global $wp_rewrite, $posts;
     5    global $wp_rewrite, $posts, $is_IIS;
    66
    7     if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() )
     7    if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) )
    88        return;
    99
     
    2525    } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
    2626        // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
    27         if ( is_single() && get_query_var('p') ) {
     27        if ( is_single() && isset($_GET['p']) ) {
    2828            if ( $redirect_url = get_permalink(get_query_var('p')) )
    2929                $redirect['query'] = remove_query_arg('p', $redirect['query']);
    30         } elseif ( is_page() && get_query_var('page_id') ) {
     30        } elseif ( is_page() && isset($_GET['page_id']) ) {
    3131            if ( $redirect_url = get_permalink(get_query_var('page_id')) )
    3232                $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
    33         } elseif ( get_query_var('m') && ( is_year() || is_month() || is_day() ) ) {
     33        } elseif ( isset($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
    3434            $m = get_query_var('m');
    3535            switch ( strlen($m) ) {
     
    4747                $redirect['query'] = remove_query_arg('m', $redirect['query']);
    4848        // now moving on to non ?m=X year/month/day links
    49         } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && get_query_var('day') ) {
     49        } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && isset($_GET['day']) ) {
    5050            if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
    5151                $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
    52         } elseif ( is_month() && get_query_var('year') && get_query_var('monthnum') ) {
     52        } elseif ( is_month() && get_query_var('year') && isset($_GET['monthnum']) ) {
    5353            if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
    5454                $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
    55         } elseif ( is_year() && get_query_var('year') ) {
     55        } elseif ( is_year() && isset($_GET['year']) ) {
    5656            if ( $redirect_url = get_year_link(get_query_var('year')) )
    5757                $redirect['query'] = remove_query_arg('year', $redirect['query']);
    58         } elseif ( is_category() && get_query_var('cat') ) {
     58        } elseif ( is_category() && isset($_GET['cat']) ) {
    5959            if ( $redirect_url = get_category_link(get_query_var('cat')) )
    6060                $redirect['query'] = remove_query_arg('cat', $redirect['query']);
    61         } elseif ( is_author() && get_query_var('author') ) {
     61        } elseif ( is_author() && isset($_GET['author']) ) {
    6262            $author = get_userdata(get_query_var('author'));
    6363            if ( false !== $author && $redirect_url = get_author_link(false, $author->ID, $author->user_nicename) )
     
    6767    // paging
    6868        if ( $paged = get_query_var('paged') ) {
    69             if ( $paged > 1 ) {
     69            if ( $paged > 0 ) {
    7070                if ( !$redirect_url )
    7171                    $redirect_url = $requested_url;
    7272                $paged_redirect = @parse_url($redirect_url);
    73                 $paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']);
    74                 $paged_redirect['path'] = trailingslashit($paged_redirect['path']);
    75                 if ( $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
    76                     $paged_redirect['path'] .= 'index.php/';
    77                 $paged_redirect['path'] .= user_trailingslashit("page/$paged");
     73                $paged_redirect['path'] = preg_replace('|/page/[0-9]+?/?$|', '/', $paged_redirect['path']); // strip off any existing paging
     74                $paged_redirect['path'] = preg_replace('|/index.php/?$|', '/', $paged_redirect['path']); // strip off trailing /index.php/
     75                if ( $paged > 1 && !is_singular() ) {
     76                    $paged_redirect['path'] = trailingslashit($paged_redirect['path']);
     77                    if ( $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
     78                        $paged_redirect['path'] .= 'index.php/';
     79                    $paged_redirect['path'] .= user_trailingslashit("page/$paged", 'paged');
     80                } elseif ( !is_home() && !is_singular() ){
     81                    $paged_redirect['path'] = user_trailingslashit($paged_redirect['path'], 'paged');
     82                }
    7883                $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
     84                $redirect['path'] = $paged_redirect['path'];
    7985            }
    8086            $redirect['query'] = remove_query_arg('paged', $redirect['query']);
     
    8288    }
    8389
    84     // tack on any additional query vars
    85     if ( $redirect_url && $redirect['query'] ) {
    86         if ( strpos($redirect_url, '?') !== false )
    87             $redirect_url .= '&';
    88         else
    89             $redirect_url .= '?';
    90         $redirect_url .= $redirect['query'];
     90// tack on any additional query vars
     91if ( $redirect_url && $redirect['query'] ) {
     92    if ( strpos($redirect_url, '?') !== false )
     93        $redirect_url .= '&';
     94    else
     95        $redirect_url .= '?';
     96    $redirect_url .= $redirect['query'];
     97}
     98
     99if ( $redirect_url )
     100    $redirect = @parse_url($redirect_url);
     101
     102    // www.example.com vs example.com
     103    $user_home = @parse_url(get_option('home'));
     104    $redirect['host'] = $user_home['host'];
     105
     106    // trailing /index.php or /index.php/
     107    $redirect['path'] = preg_replace('|/index.php/?$|', '/', $redirect['path']);
     108
     109    // strip /index.php/ when we're not using PATHINFO permalinks
     110    if ( !$wp_rewrite->using_index_permalinks() )
     111        $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
     112
     113    // trailing slashes
     114    if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_home() || ( is_home() && (get_query_var('paged') > 1) ) ) ) {
     115        $user_ts_type = '';
     116        if ( get_query_var('paged') > 0 ) {
     117            $user_ts_type = 'paged';
     118        } else {
     119            foreach ( array('single', 'category', 'page', 'day', 'month', 'year') as $type ) {
     120                $func = 'is_' . $type;
     121                if ( call_user_func($func) )
     122                    $user_ts_type = $type;
     123                    break;
     124                }
     125            }
     126        $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
    91127    }
    92128
    93     if ( !$redirect_url ) { // we're only going down this road if we don't have a WP-generated link from above
    94 
    95         // www.example.com vs example.com
    96         $user_home = parse_url(get_option('home'));
    97         $redirect['host'] = $user_home['host'];
    98 
    99         // trailing /index.php or /index.php/
    100         $redirect['path'] = preg_replace('|/index.php/?$|', '/', $redirect['path']);
    101 
    102         // need to strip off the query string
    103         if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_home() || ( is_home() && is_paged() ) ) )
    104             $redirect['path'] = user_trailingslashit($redirect['path']);
    105 
    106         if ( array($original['host'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['path'], $redirect['query']) ) {
    107             $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
    108             if ( $redirect['query'] )
    109                 $redirect_url .= '?' . $redirect['query'];
    110         }
     129    if ( array($original['host'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['path'], $redirect['query']) ) {
     130        $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
     131        if ( $redirect['query'] )
     132            $redirect_url .= '?' . $redirect['query'];
    111133    }
    112134
Note: See TracChangeset for help on using the changeset viewer.