-
+ DFC8B8492EEEB9C10C60145D705ED4825D6D2908E4E20648FE0124AA80F9C8A3BC3413C6E00989B60D9C104EFD0760E292569C1D86733E9B41D2F23AC3CF50E1
mp-wp/wp-includes/canonical.php
(0 . 0)(1 . 339)
72843 <?php
72844 /**
72845 * Canonical API to handle WordPress Redirecting
72846 *
72847 * Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference"
72848 * by Mark Jaquith
72849 *
72850 * @author Scott Yang
72851 * @author Mark Jaquith
72852 * @package WordPress
72853 * @since 2.3.0
72854 */
72855
72856 /**
72857 * Redirects incoming links to the proper URL based on the site url.
72858 *
72859 * Search engines consider www.somedomain.com and somedomain.com to be two
72860 * different URLs when they both go to the same location. This SEO enhancement
72861 * prevents penality for duplicate content by redirecting all incoming links to
72862 * one or the other.
72863 *
72864 * Prevents redirection for feeds, trackbacks, searches, comment popup, and
72865 * admin URLs. Does not redirect on IIS, page/post previews, and on form data.
72866 *
72867 * Will also attempt to find the correct link when a user enters a URL that does
72868 * not exist based on exact WordPress query. Will instead try to parse the URL
72869 * or query in an attempt to figure the correct page to go to.
72870 *
72871 * @since 2.3.0
72872 * @uses $wp_rewrite
72873 * @uses $is_IIS
72874 *
72875 * @param string $requested_url Optional. The URL that was requested, used to
72876 * figure if redirect is needed.
72877 * @param bool $do_redirect Optional. Redirect to the new URL.
72878 * @return null|false|string Null, if redirect not needed. False, if redirect
72879 * not needed or the string of the URL
72880 */
72881 function redirect_canonical($requested_url=null, $do_redirect=true) {
72882 global $wp_rewrite, $is_IIS, $wp_query, $wpdb;
72883
72884 if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() || is_robots() )
72885 return;
72886
72887 if ( !$requested_url ) {
72888 // build the URL in the address bar
72889 $requested_url = ( !empty($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
72890 $requested_url .= $_SERVER['HTTP_HOST'];
72891 $requested_url .= $_SERVER['REQUEST_URI'];
72892 }
72893
72894 $original = @parse_url($requested_url);
72895 if ( false === $original )
72896 return;
72897
72898 // Some PHP setups turn requests for / into /index.php in REQUEST_URI
72899 // See: http://trac.wordpress.org/ticket/5017
72900 // See: http://trac.wordpress.org/ticket/7173
72901 // Disabled, for now:
72902 // $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
72903
72904 $redirect = $original;
72905 $redirect_url = false;
72906
72907 // Notice fixing
72908 if ( !isset($redirect['path']) ) $redirect['path'] = '';
72909 if ( !isset($redirect['query']) ) $redirect['query'] = '';
72910
72911 if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) {
72912
72913 $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) );
72914
72915 if ( isset($vars[0]) && $vars = $vars[0] ) {
72916 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 )
72917 $id = $vars->post_parent;
72918
72919 if ( $redirect_url = get_permalink($id) )
72920 $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id'), $redirect['query']);
72921 }
72922 }
72923
72924 // These tests give us a WP-generated permalink
72925 if ( is_404() ) {
72926 $redirect_url = redirect_guess_404_permalink();
72927 } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
72928 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
72929 if ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
72930 if ( $redirect_url = get_permalink(get_query_var('p')) )
72931 $redirect['query'] = remove_query_arg('p', $redirect['query']);
72932 if ( get_query_var( 'page' ) ) {
72933 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
72934 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
72935 }
72936 } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
72937 if ( $redirect_url = get_permalink(get_query_var('page_id')) )
72938 $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
72939 } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
72940 $m = get_query_var('m');
72941 switch ( strlen($m) ) {
72942 case 4: // Yearly
72943 $redirect_url = get_year_link($m);
72944 break;
72945 case 6: // Monthly
72946 $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
72947 break;
72948 case 8: // Daily
72949 $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
72950 break;
72951 }
72952 if ( $redirect_url )
72953 $redirect['query'] = remove_query_arg('m', $redirect['query']);
72954 // now moving on to non ?m=X year/month/day links
72955 } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) {
72956 if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
72957 $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
72958 } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) {
72959 if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
72960 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
72961 } elseif ( is_year() && !empty($_GET['year']) ) {
72962 if ( $redirect_url = get_year_link(get_query_var('year')) )
72963 $redirect['query'] = remove_query_arg('year', $redirect['query']);
72964 } elseif ( is_category() && !empty($_GET['cat']) ) {
72965 if ( $redirect_url = get_category_link(get_query_var('cat')) )
72966 $redirect['query'] = remove_query_arg('cat', $redirect['query']);
72967 } elseif ( is_author() && !empty($_GET['author']) ) {
72968 $author = get_userdata(get_query_var('author'));
72969 if ( false !== $author && $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) )
72970 $redirect['query'] = remove_query_arg('author', $redirect['author']);
72971 }
72972
72973 // paging and feeds
72974 if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
72975 if ( !$redirect_url )
72976 $redirect_url = $requested_url;
72977 $paged_redirect = @parse_url($redirect_url);
72978 while ( preg_match( '#/page/[0-9]+?(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $paged_redirect['path'] ) || preg_match( '#/comment-page-[0-9]+(/+)?$#', $paged_redirect['path'] ) ) {
72979 // Strip off paging and feed
72980 $paged_redirect['path'] = preg_replace('#/page/[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing paging
72981 $paged_redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+)?$#', '/', $paged_redirect['path']); // strip off feed endings
72982 $paged_redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing comment paging
72983 }
72984
72985 $addl_path = '';
72986 if ( is_feed() ) {
72987 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
72988 if ( get_query_var( 'withcomments' ) )
72989 $addl_path .= 'comments/';
72990 $addl_path .= user_trailingslashit( 'feed/' . ( ( 'rss2' == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
72991 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
72992 }
72993
72994 if ( get_query_var('paged') > 0 ) {
72995 $paged = get_query_var('paged');
72996 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
72997 if ( !is_feed() ) {
72998 if ( $paged > 1 && !is_single() ) {
72999 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("page/$paged", 'paged');
73000 } elseif ( !is_single() ) {
73001 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit($paged_redirect['path'], 'paged');
73002 }
73003 } elseif ( $paged > 1 ) {
73004 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
73005 }
73006 }
73007
73008 if ( get_option('page_comments') && ( ( 'newest' == get_option('default_comments_page') && get_query_var('cpage') > 0 ) || ( 'newest' != get_option('default_comments_page') && get_query_var('cpage') > 1 ) ) ) {
73009 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' );
73010 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
73011 }
73012
73013 $paged_redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $paged_redirect['path']) ); // strip off trailing /index.php/
73014 if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
73015 $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . 'index.php/';
73016 if ( !empty( $addl_path ) )
73017 $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . $addl_path;
73018 $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
73019 $redirect['path'] = $paged_redirect['path'];
73020 }
73021 }
73022
73023 // tack on any additional query vars
73024 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
73025 if ( $redirect_url && !empty($redirect['query']) ) {
73026 if ( strpos($redirect_url, '?') !== false )
73027 $redirect_url .= '&';
73028 else
73029 $redirect_url .= '?';
73030 $redirect_url .= $redirect['query'];
73031 }
73032
73033 if ( $redirect_url )
73034 $redirect = @parse_url($redirect_url);
73035
73036 // www.example.com vs example.com
73037 $user_home = @parse_url(get_option('home'));
73038 if ( !empty($user_home['host']) )
73039 $redirect['host'] = $user_home['host'];
73040 if ( empty($user_home['path']) )
73041 $user_home['path'] = '/';
73042
73043 // Handle ports
73044 if ( !empty($user_home['port']) )
73045 $redirect['port'] = $user_home['port'];
73046 else
73047 unset($redirect['port']);
73048
73049 // trailing /index.php
73050 $redirect['path'] = preg_replace('|/index.php/*?$|', '/', $redirect['path']);
73051
73052 // Remove trailing spaces from the path
73053 $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] );
73054
73055 if ( !empty( $redirect['query'] ) ) {
73056 // Remove trailing spaces from certain terminating query string args
73057 $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] );
73058
73059 // Clean up empty query strings
73060 $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
73061
73062 // Remove redundant leading ampersands
73063 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
73064 }
73065
73066 // strip /index.php/ when we're not using PATHINFO permalinks
73067 if ( !$wp_rewrite->using_index_permalinks() )
73068 $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
73069
73070 // trailing slashes
73071 if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) {
73072 $user_ts_type = '';
73073 if ( get_query_var('paged') > 0 ) {
73074 $user_ts_type = 'paged';
73075 } else {
73076 foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) {
73077 $func = 'is_' . $type;
73078 if ( call_user_func($func) ) {
73079 $user_ts_type = $type;
73080 break;
73081 }
73082 }
73083 }
73084 $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
73085 } elseif ( is_front_page() ) {
73086 $redirect['path'] = trailingslashit($redirect['path']);
73087 }
73088
73089 // Always trailing slash the Front Page URL
73090 if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
73091 $redirect['path'] = trailingslashit($redirect['path']);
73092
73093 // Ignore differences in host capitalization, as this can lead to infinite redirects
73094 // Only redirect no-www <=> yes-www
73095 if ( strtolower($original['host']) == strtolower($redirect['host']) ||
73096 ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
73097 $redirect['host'] = $original['host'];
73098
73099 $compare_original = array($original['host'], $original['path']);
73100
73101 if ( !empty( $original['port'] ) )
73102 $compare_original[] = $original['port'];
73103
73104 if ( !empty( $original['query'] ) )
73105 $compare_original[] = $original['query'];
73106
73107 $compare_redirect = array($redirect['host'], $redirect['path']);
73108
73109 if ( !empty( $redirect['port'] ) )
73110 $compare_redirect[] = $redirect['port'];
73111
73112 if ( !empty( $redirect['query'] ) )
73113 $compare_redirect[] = $redirect['query'];
73114
73115 if ( $compare_original !== $compare_redirect ) {
73116 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
73117 if ( !empty($redirect['port']) )
73118 $redirect_url .= ':' . $redirect['port'];
73119 $redirect_url .= $redirect['path'];
73120 if ( !empty($redirect['query']) )
73121 $redirect_url .= '?' . $redirect['query'];
73122 }
73123
73124 if ( !$redirect_url || $redirect_url == $requested_url )
73125 return false;
73126
73127 // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE
73128 $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
73129
73130 if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
73131 return false;
73132
73133 if ( $do_redirect ) {
73134 // protect against chained redirects
73135 if ( !redirect_canonical($redirect_url, false) ) {
73136 wp_redirect($redirect_url, 301);
73137 exit();
73138 } else {
73139 // Debug
73140 // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
73141 return false;
73142 }
73143 } else {
73144 return $redirect_url;
73145 }
73146 }
73147
73148 /**
73149 * Attempts to guess correct post based on query vars.
73150 *
73151 * @since 2.3.0
73152 * @uses $wpdb
73153 *
73154 * @return bool|string Returns False, if it can't find post, returns correct
73155 * location on success.
73156 */
73157 function redirect_guess_404_permalink() {
73158 global $wpdb;
73159
73160 if ( !get_query_var('name') )
73161 return false;
73162
73163 $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
73164
73165 // if any of year, monthnum, or day are set, use them to refine the query
73166 if ( get_query_var('year') )
73167 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
73168 if ( get_query_var('monthnum') )
73169 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
73170 if ( get_query_var('day') )
73171 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
73172
73173 $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
73174 if ( !$post_id )
73175 return false;
73176 return get_permalink($post_id);
73177 }
73178
73179 add_action('template_redirect', 'redirect_canonical');
73180
73181 ?>