wp/wp-includes/canonical.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
    15  * Search engines consider www.somedomain.com and somedomain.com to be two
    15  * Search engines consider www.somedomain.com and somedomain.com to be two
    16  * different URLs when they both go to the same location. This SEO enhancement
    16  * different URLs when they both go to the same location. This SEO enhancement
    17  * prevents penalty for duplicate content by redirecting all incoming links to
    17  * prevents penalty for duplicate content by redirecting all incoming links to
    18  * one or the other.
    18  * one or the other.
    19  *
    19  *
    20  * Prevents redirection for feeds, trackbacks, searches, comment popup, and
    20  * Prevents redirection for feeds, trackbacks, searches, and
    21  * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+,
    21  * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+,
    22  * page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST
    22  * page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST
    23  * requests.
    23  * requests.
    24  *
    24  *
    25  * Will also attempt to find the correct link when a user enters a URL that does
    25  * Will also attempt to find the correct link when a user enters a URL that does
    26  * not exist based on exact WordPress query. Will instead try to parse the URL
    26  * not exist based on exact WordPress query. Will instead try to parse the URL
    27  * or query in an attempt to figure the correct page to go to.
    27  * or query in an attempt to figure the correct page to go to.
    28  *
    28  *
    29  * @since 2.3.0
    29  * @since 2.3.0
    30  * @uses $wp_rewrite
    30  *
    31  * @uses $is_IIS
    31  * @global WP_Rewrite $wp_rewrite
       
    32  * @global bool $is_IIS
       
    33  * @global WP_Query $wp_query
       
    34  * @global wpdb $wpdb WordPress database abstraction object.
       
    35  * @global WP $wp Current WordPress environment instance. 
    32  *
    36  *
    33  * @param string $requested_url Optional. The URL that was requested, used to
    37  * @param string $requested_url Optional. The URL that was requested, used to
    34  *		figure if redirect is needed.
    38  *		figure if redirect is needed.
    35  * @param bool $do_redirect Optional. Redirect to the new URL.
    39  * @param bool $do_redirect Optional. Redirect to the new URL.
    36  * @return null|false|string Null, if redirect not needed. False, if redirect
    40  * @return string|void The string of the URL, if redirect needed.
    37  *		not needed or the string of the URL
       
    38  */
    41  */
    39 function redirect_canonical( $requested_url = null, $do_redirect = true ) {
    42 function redirect_canonical( $requested_url = null, $do_redirect = true ) {
    40 	global $wp_rewrite, $is_IIS, $wp_query, $wpdb;
    43 	global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp;
    41 
    44 
    42 	if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) {
    45 	if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) {
    43 		return;
    46 		return;
    44 	}
    47 	}
    45 
    48 
    51 			|| ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) {
    54 			|| ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) {
    52 			$wp_query->is_preview = false;
    55 			$wp_query->is_preview = false;
    53 		}
    56 		}
    54 	}
    57 	}
    55 
    58 
    56 	if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || is_preview() || is_robots() || ( $is_IIS && !iis7_supports_permalinks() ) ) {
    59 	if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && !iis7_supports_permalinks() ) ) {
    57 		return;
    60 		return;
    58 	}
    61 	}
    59 
    62 
    60 	if ( !$requested_url ) {
    63 	if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
    61 		// build the URL in the address bar
    64 		// build the URL in the address bar
    62 		$requested_url  = is_ssl() ? 'https://' : 'http://';
    65 		$requested_url  = is_ssl() ? 'https://' : 'http://';
    63 		$requested_url .= $_SERVER['HTTP_HOST'];
    66 		$requested_url .= $_SERVER['HTTP_HOST'];
    64 		$requested_url .= $_SERVER['REQUEST_URI'];
    67 		$requested_url .= $_SERVER['REQUEST_URI'];
    65 	}
    68 	}
    66 
    69 
    67 	$original = @parse_url($requested_url);
    70 	$original = @parse_url($requested_url);
    68 	if ( false === $original )
    71 	if ( false === $original ) {
    69 		return;
    72 		return;
    70 
    73 	}
    71 	// Some PHP setups turn requests for / into /index.php in REQUEST_URI
       
    72 	// See: https://core.trac.wordpress.org/ticket/5017
       
    73 	// See: https://core.trac.wordpress.org/ticket/7173
       
    74 	// Disabled, for now:
       
    75 	// $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
       
    76 
    74 
    77 	$redirect = $original;
    75 	$redirect = $original;
    78 	$redirect_url = false;
    76 	$redirect_url = false;
    79 
    77 
    80 	// Notice fixing
    78 	// Notice fixing
   118 
   116 
   119 		// Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
   117 		// Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
   120 		$id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') );
   118 		$id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') );
   121 		if ( $id && $redirect_post = get_post($id) ) {
   119 		if ( $id && $redirect_post = get_post($id) ) {
   122 			$post_type_obj = get_post_type_object($redirect_post->post_type);
   120 			$post_type_obj = get_post_type_object($redirect_post->post_type);
   123 			if ( $post_type_obj->public ) {
   121 			if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) {
   124 				$redirect_url = get_permalink($redirect_post);
   122 				$redirect_url = get_permalink($redirect_post);
   125 				$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
   123 				$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
   126 			}
   124 			}
   127 		}
   125 		}
   128 
   126 
   144 			if ( $redirect_url = redirect_guess_404_permalink() ) {
   142 			if ( $redirect_url = redirect_guess_404_permalink() ) {
   145 				$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
   143 				$redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'page', 'feed', 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
   146 			}
   144 			}
   147 		}
   145 		}
   148 
   146 
       
   147 		if ( get_query_var( 'page' ) && $wp_query->post &&
       
   148 			false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) {
       
   149 			$redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
       
   150 			$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
       
   151 			$redirect_url = get_permalink( $wp_query->post->ID );
       
   152 		}
       
   153 
   149 	} elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
   154 	} elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
   150 		// rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
   155 		// rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
   151 		if ( is_attachment() && !empty($_GET['attachment_id']) && ! $redirect_url ) {
   156 		if ( is_attachment() &&
   152 			if ( $redirect_url = get_attachment_link(get_query_var('attachment_id')) )
   157 			! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) &&
   153 				$redirect['query'] = remove_query_arg('attachment_id', $redirect['query']);
   158 			! $redirect_url ) {
       
   159 			if ( ! empty( $_GET['attachment_id'] ) ) {
       
   160 				$redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
       
   161 				if ( $redirect_url ) {
       
   162 					$redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] );
       
   163 				}
       
   164 			} else {
       
   165 				$redirect_url = get_attachment_link();
       
   166 			}
   154 		} elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
   167 		} elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
   155 			if ( $redirect_url = get_permalink(get_query_var('p')) )
   168 			if ( $redirect_url = get_permalink(get_query_var('p')) )
   156 				$redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']);
   169 				$redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']);
   157 		} elseif ( is_single() && !empty($_GET['name'])  && ! $redirect_url ) {
   170 		} elseif ( is_single() && !empty($_GET['name'])  && ! $redirect_url ) {
   158 			if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) )
   171 			if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) )
   159 				$redirect['query'] = remove_query_arg('name', $redirect['query']);
   172 				$redirect['query'] = remove_query_arg('name', $redirect['query']);
   160 		} elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
   173 		} elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
   161 			if ( $redirect_url = get_permalink(get_query_var('page_id')) )
   174 			if ( $redirect_url = get_permalink(get_query_var('page_id')) )
   162 				$redirect['query'] = remove_query_arg('page_id', $redirect['query']);
   175 				$redirect['query'] = remove_query_arg('page_id', $redirect['query']);
   163 		} elseif ( is_page() && !is_feed() && isset($wp_query->queried_object) && 'page' == get_option('show_on_front') && $wp_query->queried_object->ID == get_option('page_on_front')  && ! $redirect_url ) {
   176 		} elseif ( is_page() && !is_feed() && 'page' == get_option('show_on_front') && get_queried_object_id() == get_option('page_on_front')  && ! $redirect_url ) {
   164 			$redirect_url = home_url('/');
   177 			$redirect_url = home_url('/');
   165 		} elseif ( is_home() && !empty($_GET['page_id']) && 'page' == get_option('show_on_front') && get_query_var('page_id') == get_option('page_for_posts')  && ! $redirect_url ) {
   178 		} elseif ( is_home() && !empty($_GET['page_id']) && 'page' == get_option('show_on_front') && get_query_var('page_id') == get_option('page_for_posts')  && ! $redirect_url ) {
   166 			if ( $redirect_url = get_permalink(get_option('page_for_posts')) )
   179 			if ( $redirect_url = get_permalink(get_option('page_for_posts')) )
   167 				$redirect['query'] = remove_query_arg('page_id', $redirect['query']);
   180 				$redirect['query'] = remove_query_arg('page_id', $redirect['query']);
   168 		} elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
   181 		} elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
   242 				}
   255 				}
   243 
   256 
   244 			}
   257 			}
   245 		} elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
   258 		} elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
   246 			$category = get_category_by_path( $cat );
   259 			$category = get_category_by_path( $cat );
   247 			$post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids'));
   260 			if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) {
   248 			if ( (!$category || is_wp_error($category)) || ( !is_wp_error($post_terms) && !empty($post_terms) && !in_array($category->term_taxonomy_id, $post_terms) ) )
       
   249 				$redirect_url = get_permalink($wp_query->get_queried_object_id());
   261 				$redirect_url = get_permalink($wp_query->get_queried_object_id());
       
   262 			}
   250 		}
   263 		}
   251 
   264 
   252 		// Post Paging
   265 		// Post Paging
   253 		if ( is_singular() && ! is_front_page() && get_query_var('page') ) {
   266 		if ( is_singular() && get_query_var('page') ) {
   254 			if ( !$redirect_url )
   267 			if ( !$redirect_url )
   255 				$redirect_url = get_permalink( get_queried_object_id() );
   268 				$redirect_url = get_permalink( get_queried_object_id() );
   256 			$redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
   269 
       
   270 			$page = get_query_var( 'page' );
       
   271 			if ( $page > 1 ) {
       
   272 				if ( is_front_page() ) {
       
   273 					$redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' );
       
   274 				} else {
       
   275 					$redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' );
       
   276 				}
       
   277 			}
   257 			$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
   278 			$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
   258 		}
   279 		}
   259 
   280 
   260 		// paging and feeds
   281 		// paging and feeds
   261 		if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
   282 		if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
   304 				} elseif ( $paged > 1 ) {
   325 				} elseif ( $paged > 1 ) {
   305 					$redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
   326 					$redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
   306 				}
   327 				}
   307 			}
   328 			}
   308 
   329 
   309 			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 ) ) ) {
   330 			if ( get_option( 'page_comments' ) && (
       
   331 				( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) ||
       
   332 				( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 )
       
   333 			) ) {
   310 				$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' );
   334 				$addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' );
   311 				$redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
   335 				$redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
   312 			}
   336 			}
   313 
   337 
   314 			$redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/
   338 			$redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/
   322 		if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
   346 		if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
   323 			if ( is_multisite() ) {
   347 			if ( is_multisite() ) {
   324 				/** This filter is documented in wp-login.php */
   348 				/** This filter is documented in wp-login.php */
   325 				$redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
   349 				$redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
   326 			} else {
   350 			} else {
   327 				$redirect_url = site_url( 'wp-login.php?action=register' );
   351 				$redirect_url = wp_registration_url();
   328 			}
   352 			}
   329 
   353 
   330 			wp_redirect( $redirect_url, 301 );
   354 			wp_redirect( $redirect_url, 301 );
   331 			die();
   355 			die();
   332 		}
   356 		}
   366 		unset($redirect['port']);
   390 		unset($redirect['port']);
   367 
   391 
   368 	// trailing /index.php
   392 	// trailing /index.php
   369 	$redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']);
   393 	$redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']);
   370 
   394 
   371 	// Remove trailing spaces from the path
   395 	$punctuation_pattern = implode( '|', array_map( 'preg_quote', array(
   372 	$redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] );
   396 		' ', '%20',  // space
       
   397 		'!', '%21',  // exclamation mark
       
   398 		'"', '%22',  // double quote
       
   399 		"'", '%27',  // single quote
       
   400 		'(', '%28',  // opening bracket
       
   401 		')', '%29',  // closing bracket
       
   402 		',', '%2C',  // comma
       
   403 		'.', '%2E',  // period
       
   404 		';', '%3B',  // semicolon
       
   405 		'{', '%7B',  // opening curly bracket
       
   406 		'}', '%7D',  // closing curly bracket
       
   407 		'%E2%80%9C', // opening curly quote
       
   408 		'%E2%80%9D', // closing curly quote
       
   409 	) ) );
       
   410 
       
   411 	// Remove trailing spaces and end punctuation from the path.
       
   412 	$redirect['path'] = preg_replace( "#($punctuation_pattern)+$#", '', $redirect['path'] );
   373 
   413 
   374 	if ( !empty( $redirect['query'] ) ) {
   414 	if ( !empty( $redirect['query'] ) ) {
   375 		// Remove trailing spaces from certain terminating query string args
   415 		// Remove trailing spaces and end punctuation from certain terminating query string args.
   376 		$redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] );
   416 		$redirect['query'] = preg_replace( "#((p|page_id|cat|tag)=[^&]*?)($punctuation_pattern)+$#", '$1', $redirect['query'] );
   377 
   417 
   378 		// Clean up empty query strings
   418 		// Clean up empty query strings
   379 		$redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
   419 		$redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
   380 
   420 
   381 		// Redirect obsolete feeds
   421 		// Redirect obsolete feeds
   445 		$redirect_url .= $redirect['path'];
   485 		$redirect_url .= $redirect['path'];
   446 		if ( !empty($redirect['query']) )
   486 		if ( !empty($redirect['query']) )
   447 			$redirect_url .= '?' . $redirect['query'];
   487 			$redirect_url .= '?' . $redirect['query'];
   448 	}
   488 	}
   449 
   489 
   450 	if ( !$redirect_url || $redirect_url == $requested_url )
   490 	if ( ! $redirect_url || $redirect_url == $requested_url ) {
   451 		return false;
   491 		return;
       
   492 	}
   452 
   493 
   453 	// Hex encoded octets are case-insensitive.
   494 	// Hex encoded octets are case-insensitive.
   454 	if ( false !== strpos($requested_url, '%') ) {
   495 	if ( false !== strpos($requested_url, '%') ) {
   455 		if ( !function_exists('lowercase_octets') ) {
   496 		if ( !function_exists('lowercase_octets') ) {
       
   497 			/**
       
   498 			 * Converts the first hex-encoded octet match to lowercase.
       
   499 			 *
       
   500 			 * @since 3.1.0
       
   501 			 * @ignore
       
   502 			 *
       
   503 			 * @param array $matches Hex-encoded octet matches for the requested URL.
       
   504 			 * @return string Lowercased version of the first match.
       
   505 			 */
   456 			function lowercase_octets($matches) {
   506 			function lowercase_octets($matches) {
   457 				return strtolower( $matches[0] );
   507 				return strtolower( $matches[0] );
   458 			}
   508 			}
   459 		}
   509 		}
   460 		$requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
   510 		$requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
   461 	}
   511 	}
   462 
   512 
   463 	/**
   513 	/**
   464 	 * Filter the canonical redirect URL.
   514 	 * Filters the canonical redirect URL.
   465 	 *
   515 	 *
   466 	 * Returning false to this filter will cancel the redirect.
   516 	 * Returning false to this filter will cancel the redirect.
   467 	 *
   517 	 *
   468 	 * @since 2.3.0
   518 	 * @since 2.3.0
   469 	 *
   519 	 *
   470 	 * @param string $redirect_url  The redirect URL.
   520 	 * @param string $redirect_url  The redirect URL.
   471 	 * @param string $requested_url The requested URL.
   521 	 * @param string $requested_url The requested URL.
   472 	 */
   522 	 */
   473 	$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
   523 	$redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
   474 
   524 
   475 	if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
   525 	// yes, again -- in case the filter aborted the request
   476 		return false;
   526 	if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) {
       
   527 		return;
       
   528 	}
   477 
   529 
   478 	if ( $do_redirect ) {
   530 	if ( $do_redirect ) {
   479 		// protect against chained redirects
   531 		// protect against chained redirects
   480 		if ( !redirect_canonical($redirect_url, false) ) {
   532 		if ( !redirect_canonical($redirect_url, false) ) {
   481 			wp_redirect($redirect_url, 301);
   533 			wp_redirect($redirect_url, 301);
   482 			exit();
   534 			exit();
   483 		} else {
   535 		} else {
   484 			// Debug
   536 			// Debug
   485 			// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
   537 			// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
   486 			return false;
   538 			return;
   487 		}
   539 		}
   488 	} else {
   540 	} else {
   489 		return $redirect_url;
   541 		return $redirect_url;
   490 	}
   542 	}
   491 }
   543 }
   515 	}
   567 	}
   516 	return $query_string;
   568 	return $query_string;
   517 }
   569 }
   518 
   570 
   519 /**
   571 /**
       
   572  * Strips the #fragment from a URL, if one is present.
       
   573  *
       
   574  * @since 4.4.0
       
   575  *
       
   576  * @param string $url The URL to strip.
       
   577  * @return string The altered URL.
       
   578  */
       
   579 function strip_fragment_from_url( $url ) {
       
   580 	$parsed_url = @parse_url( $url );
       
   581 	if ( ! empty( $parsed_url['host'] ) ) {
       
   582 		// This mirrors code in redirect_canonical(). It does not handle every case.
       
   583 		$url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
       
   584 		if ( ! empty( $parsed_url['port'] ) ) {
       
   585 			$url .= ':' . $parsed_url['port'];
       
   586 		}
       
   587 
       
   588 		if ( ! empty( $parsed_url['path'] ) ) {
       
   589 			$url .= $parsed_url['path'];
       
   590 		}
       
   591 
       
   592 		if ( ! empty( $parsed_url['query'] ) ) {
       
   593 			$url .= '?' . $parsed_url['query'];
       
   594 		}
       
   595 	}
       
   596 
       
   597 	return $url;
       
   598 }
       
   599 
       
   600 /**
   520  * Attempts to guess the correct URL based on query vars
   601  * Attempts to guess the correct URL based on query vars
   521  *
   602  *
   522  * @since 2.3.0
   603  * @since 2.3.0
   523  *
   604  *
   524  * @global wpdb $wpdb WordPress database abstraction object.
   605  * @global wpdb $wpdb WordPress database abstraction object.
   525  *
   606  *
   526  * @return bool|string The correct URL if one is found. False on failure.
   607  * @return false|string The correct URL if one is found. False on failure.
   527  */
   608  */
   528 function redirect_guess_404_permalink() {
   609 function redirect_guess_404_permalink() {
   529 	global $wpdb, $wp_rewrite;
   610 	global $wpdb;
   530 
   611 
   531 	if ( get_query_var('name') ) {
   612 	if ( get_query_var('name') ) {
   532 		$where = $wpdb->prepare("post_name LIKE %s", $wpdb->esc_like( get_query_var('name') ) . '%');
   613 		$where = $wpdb->prepare("post_name LIKE %s", $wpdb->esc_like( get_query_var('name') ) . '%');
   533 
   614 
   534 		// if any of post_type, year, monthnum, or day are set, use them to refine the query
   615 		// if any of post_type, year, monthnum, or day are set, use them to refine the query
   547 		$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
   628 		$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
   548 		if ( ! $post_id )
   629 		if ( ! $post_id )
   549 			return false;
   630 			return false;
   550 		if ( get_query_var( 'feed' ) )
   631 		if ( get_query_var( 'feed' ) )
   551 			return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
   632 			return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
   552 		elseif ( get_query_var( 'page' ) )
   633 		elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) )
   553 			return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
   634 			return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
   554 		else
   635 		else
   555 			return get_permalink( $post_id );
   636 			return get_permalink( $post_id );
   556 	}
   637 	}
   557 
   638 
   558 	return false;
   639 	return false;
   559 }
   640 }
   560 
   641 
       
   642 /**
       
   643  * Redirects a variety of shorthand URLs to the admin.
       
   644  *
       
   645  * If a user visits example.com/admin, they'll be redirected to /wp-admin.
       
   646  * Visiting /login redirects to /wp-login.php, and so on.
       
   647  *
       
   648  * @since 3.4.0
       
   649  *
       
   650  * @global WP_Rewrite $wp_rewrite
       
   651  */
   561 function wp_redirect_admin_locations() {
   652 function wp_redirect_admin_locations() {
   562 	global $wp_rewrite;
   653 	global $wp_rewrite;
   563 	if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
   654 	if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
   564 		return;
   655 		return;
   565 
   656 
   579 		home_url( 'wp-login.php', 'relative' ),
   670 		home_url( 'wp-login.php', 'relative' ),
   580 		home_url( 'login', 'relative' ),
   671 		home_url( 'login', 'relative' ),
   581 		site_url( 'login', 'relative' ),
   672 		site_url( 'login', 'relative' ),
   582 	);
   673 	);
   583 	if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
   674 	if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
   584 		wp_redirect( site_url( 'wp-login.php', 'login' ) );
   675 		wp_redirect( wp_login_url() );
   585 		exit;
   676 		exit;
   586 	}
   677 	}
   587 }
   678 }