diff options
author | donncha <donncha@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2008-04-04 16:44:15 +0000 |
---|---|---|
committer | donncha <donncha@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2008-04-04 16:44:15 +0000 |
commit | 7740e89de3e1bc0cc636120e3ca8ab9e97e4d3cc (patch) | |
tree | c6fd23b598f3994eddb18cb1c0f2e8d95ff054fa /wp-includes/canonical.php | |
parent | f650f48c048bfbbb2ae702b6425d87e39358d748 (diff) | |
download | wordpress-mu-7740e89de3e1bc0cc636120e3ca8ab9e97e4d3cc.tar.gz wordpress-mu-7740e89de3e1bc0cc636120e3ca8ab9e97e4d3cc.tar.xz wordpress-mu-7740e89de3e1bc0cc636120e3ca8ab9e97e4d3cc.zip |
Merged with WordPress 2.5, unstable, only for testing
git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@1218 7be80a69-a1ef-0310-a953-fb0f7c49ff36
Diffstat (limited to 'wp-includes/canonical.php')
-rw-r--r-- | wp-includes/canonical.php | 86 |
1 files changed, 64 insertions, 22 deletions
diff --git a/wp-includes/canonical.php b/wp-includes/canonical.php index 9226f3e..5ad4457 100644 --- a/wp-includes/canonical.php +++ b/wp-includes/canonical.php @@ -1,8 +1,39 @@ <?php -// Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" by Mark Jaquith - -function redirect_canonical($requested_url=NULL, $do_redirect=true) { - global $wp_rewrite, $posts, $is_IIS; +/** + * Canonical API to handle WordPress Redirecting + * + * Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference" by Mark Jaquith + * + * @author Scott Yang + * @author Mark Jaquith + * @package WordPress + * @since 2.3 + */ + +/** + * redirect_canonical() - Redirects incoming links to the proper URL based on the site url + * + * Search engines consider www.somedomain.com and somedomain.com to be two different URLs + * when they both go to the same location. This SEO enhancement prevents penality for + * duplicate content by redirecting all incoming links to one or the other. + * + * Prevents redirection for feeds, trackbacks, searches, comment popup, and admin URLs. + * Does not redirect on IIS, page/post previews, and on form data. + * + * Will also attempt to find the correct link when a user enters a URL that does not exist + * based on exact WordPress query. Will instead try to parse the URL or query in an attempt + * to figure the correct page to go to. + * + * @since 2.3 + * @uses $wp_rewrite + * @uses $is_IIS + * + * @param string $requested_url Optional. The URL that was requested, used to figure if redirect is needed. + * @param bool $do_redirect Optional. Redirect to the new URL. + * @return null|false|string Null, if redirect not needed. False, if redirect not needed or the string of the URL + */ +function redirect_canonical($requested_url=null, $do_redirect=true) { + global $wp_rewrite, $is_IIS; if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() ) return; @@ -156,39 +187,50 @@ function redirect_canonical($requested_url=NULL, $do_redirect=true) { $redirect_url .= '?' . $redirect['query']; } - if ( $redirect_url && $redirect_url != $requested_url ) { - // var_dump($redirect_url); die(); - $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url); - if ( $do_redirect) { - // protect against chained redirects - if ( !redirect_canonical($redirect_url, false) ) { - wp_redirect($redirect_url, 301); - exit(); - } else { - return false; - } + if ( !$redirect_url || $redirect_url == $requested_url ) + return false; + + // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE + $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url); + + if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request + return false; + + if ( $do_redirect ) { + // protect against chained redirects + if ( !redirect_canonical($redirect_url, false) ) { + wp_redirect($redirect_url, 301); + exit(); } else { - return $redirect_url; + return false; } } else { - return false; + return $redirect_url; } } +/** + * redirect_guess_404_permalink() - Tries to guess correct post based on query vars + * + * @since 2.3 + * @uses $wpdb + * + * @return bool|string Returns False, if it can't find post, returns correct location on success. + */ function redirect_guess_404_permalink() { - global $wp_query, $wpdb; + global $wpdb; if ( !get_query_var('name') ) return false; - $where = "post_name LIKE '" . $wpdb->escape(get_query_var('name')) . "%'"; + $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%'); // if any of year, monthnum, or day are set, use them to refine the query if ( get_query_var('year') ) - $where .= " AND YEAR(post_date) = '" . $wpdb->escape(get_query_var('year')) . "'"; + $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); if ( get_query_var('monthnum') ) - $where .= " AND MONTH(post_date) = '" . $wpdb->escape(get_query_var('monthnum')) . "'"; + $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); if ( get_query_var('day') ) - $where .= " AND DAYOFMONTH(post_date) = '" . $wpdb->escape(get_query_var('day')) . "'"; + $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); if ( !$post_id ) |