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

git-svn-id: https://develop.svn.wordpress.org/trunk@6085 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Mark Jaquith 2007-09-11 21:21:40 +00:00
parent e0a59804ee
commit 7eea02c12c
1 changed files with 24 additions and 7 deletions

View File

@ -1,16 +1,18 @@
<?php
// Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" by Mark Jaquith
function redirect_canonical() {
function redirect_canonical($requested_url=NULL, $do_redirect=true) {
global $wp_rewrite, $posts, $is_IIS;
if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) )
return;
// build the URL in the address bar
$requested_url = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
$requested_url .= $_SERVER['HTTP_HOST'];
$requested_url .= $_SERVER['REQUEST_URI'];
if ( !$requested_url ) {
// build the URL in the address bar
$requested_url = ( isset($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
$requested_url .= $_SERVER['HTTP_HOST'];
$requested_url .= $_SERVER['REQUEST_URI'];
}
$original = @parse_url($requested_url);
if ( false === $original )
@ -126,6 +128,10 @@ if ( $redirect_url )
$redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
}
// Always trailing slash the 'home' URL
if ( $redirect['path'] == $user_home['path'] )
$redirect['path'] = trailingslashit($redirect['path']);
if ( array($original['host'], $original['path'], $original['query']) !== array($redirect['host'], $redirect['path'], $redirect['query']) ) {
$redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
if ( $redirect['query'] )
@ -135,8 +141,19 @@ if ( $redirect_url )
if ( $redirect_url && $redirect_url != $requested_url ) {
// var_dump($redirect_url); die();
$redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
wp_redirect($redirect_url, 301);
exit();
if ( $do_redirect) {
// protect against chained redirects
if ( !redirect_canonical($redirect_url, false) ) {
wp_redirect($redirect_url, 301);
exit();
} else {
return false;
}
} else {
return $redirect_url;
}
} else {
return false;
}
}