3 * Canonical API to handle WordPress Redirecting
5 * Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference"
15 * Redirects incoming links to the proper URL based on the site url.
17 * Search engines consider www.somedomain.com and somedomain.com to be two
18 * different URLs when they both go to the same location. This SEO enhancement
19 * prevents penality for duplicate content by redirecting all incoming links to
22 * Prevents redirection for feeds, trackbacks, searches, comment popup, and
23 * admin URLs. Does not redirect on IIS, page/post previews, and on form data.
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
27 * or query in an attempt to figure the correct page to go to.
33 * @param string $requested_url Optional. The URL that was requested, used to
34 * figure if redirect is needed.
35 * @param bool $do_redirect Optional. Redirect to the new URL.
36 * @return null|false|string Null, if redirect not needed. False, if redirect
37 * not needed or the string of the URL
39 function redirect_canonical($requested_url=null, $do_redirect=true) {
40 global $wp_rewrite, $is_IIS, $wp_query, $wpdb;
42 if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() || is_robots() )
45 if ( !$requested_url ) {
46 // build the URL in the address bar
47 $requested_url = ( !empty($_SERVER['HTTPS'] ) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
48 $requested_url .= $_SERVER['HTTP_HOST'];
49 $requested_url .= $_SERVER['REQUEST_URI'];
52 $original = @parse_url($requested_url);
53 if ( false === $original )
56 // Some PHP setups turn requests for / into /index.php in REQUEST_URI
57 // See: http://trac.wordpress.org/ticket/5017
58 // See: http://trac.wordpress.org/ticket/7173
60 // $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
62 $redirect = $original;
63 $redirect_url = false;
66 if ( !isset($redirect['path']) ) $redirect['path'] = '';
67 if ( !isset($redirect['query']) ) $redirect['query'] = '';
69 if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) {
71 $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) );
73 if ( isset($vars[0]) && $vars = $vars[0] ) {
74 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 )
75 $id = $vars->post_parent;
77 if ( $redirect_url = get_permalink($id) )
78 $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id'), $redirect['query']);
82 // These tests give us a WP-generated permalink
84 $redirect_url = redirect_guess_404_permalink();
85 } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
86 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
87 if ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
88 if ( $redirect_url = get_permalink(get_query_var('p')) )
89 $redirect['query'] = remove_query_arg('p', $redirect['query']);
90 if ( get_query_var( 'page' ) ) {
91 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
92 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
94 } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
95 if ( $redirect_url = get_permalink(get_query_var('page_id')) )
96 $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
97 } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
98 $m = get_query_var('m');
99 switch ( strlen($m) ) {
101 $redirect_url = get_year_link($m);
104 $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
107 $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
111 $redirect['query'] = remove_query_arg('m', $redirect['query']);
112 // now moving on to non ?m=X year/month/day links
113 } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) {
114 if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
115 $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
116 } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) {
117 if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
118 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
119 } elseif ( is_year() && !empty($_GET['year']) ) {
120 if ( $redirect_url = get_year_link(get_query_var('year')) )
121 $redirect['query'] = remove_query_arg('year', $redirect['query']);
122 } elseif ( is_category() && !empty($_GET['cat']) && preg_match( '|^[0-9]+$|', $_GET['cat'] ) ) {
123 if ( $redirect_url = get_category_link(get_query_var('cat')) )
124 $redirect['query'] = remove_query_arg('cat', $redirect['query']);
125 } elseif ( is_author() && !empty($_GET['author']) ) {
126 $author = get_userdata(get_query_var('author'));
127 if ( false !== $author && $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) )
128 $redirect['query'] = remove_query_arg('author', $redirect['author']);
132 if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
133 if ( !$redirect_url )
134 $redirect_url = $requested_url;
135 $paged_redirect = @parse_url($redirect_url);
136 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'] ) ) {
137 // Strip off paging and feed
138 $paged_redirect['path'] = preg_replace('#/page/[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing paging
139 $paged_redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $paged_redirect['path']); // strip off feed endings
140 $paged_redirect['path'] = preg_replace('#/comment-page-[0-9]+?(/+)?$#', '/', $paged_redirect['path']); // strip off any existing comment paging
145 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
146 if ( get_query_var( 'withcomments' ) )
147 $addl_path .= 'comments/';
148 $addl_path .= user_trailingslashit( 'feed/' . ( ( 'rss2' == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
149 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
152 if ( get_query_var('paged') > 0 ) {
153 $paged = get_query_var('paged');
154 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
156 if ( $paged > 1 && !is_single() ) {
157 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("page/$paged", 'paged');
158 } elseif ( !is_single() ) {
159 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit($paged_redirect['path'], 'paged');
161 } elseif ( $paged > 1 ) {
162 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
166 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 ) ) ) {
167 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( 'comment-page-' . get_query_var('cpage'), 'commentpaged' );
168 $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
171 $paged_redirect['path'] = user_trailingslashit( preg_replace('|/index.php/?$|', '/', $paged_redirect['path']) ); // strip off trailing /index.php/
172 if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($paged_redirect['path'], '/index.php/') === false )
173 $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . 'index.php/';
174 if ( !empty( $addl_path ) )
175 $paged_redirect['path'] = trailingslashit($paged_redirect['path']) . $addl_path;
176 $redirect_url = $paged_redirect['scheme'] . '://' . $paged_redirect['host'] . $paged_redirect['path'];
177 $redirect['path'] = $paged_redirect['path'];
181 // tack on any additional query vars
182 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
183 if ( $redirect_url && !empty($redirect['query']) ) {
184 if ( strpos($redirect_url, '?') !== false )
185 $redirect_url .= '&';
187 $redirect_url .= '?';
188 $redirect_url .= $redirect['query'];
192 $redirect = @parse_url($redirect_url);
194 // www.example.com vs example.com
195 $user_home = @parse_url(get_option('home'));
196 if ( !empty($user_home['host']) )
197 $redirect['host'] = $user_home['host'];
198 if ( empty($user_home['path']) )
199 $user_home['path'] = '/';
202 if ( !empty($user_home['port']) )
203 $redirect['port'] = $user_home['port'];
205 unset($redirect['port']);
207 // trailing /index.php
208 $redirect['path'] = preg_replace('|/index.php/*?$|', '/', $redirect['path']);
210 // Remove trailing spaces from the path
211 $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] );
213 if ( !empty( $redirect['query'] ) ) {
214 // Remove trailing spaces from certain terminating query string args
215 $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] );
217 // Clean up empty query strings
218 $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
220 // Remove redundant leading ampersands
221 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
224 // strip /index.php/ when we're not using PATHINFO permalinks
225 if ( !$wp_rewrite->using_index_permalinks() )
226 $redirect['path'] = str_replace('/index.php/', '/', $redirect['path']);
229 if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) {
231 if ( get_query_var('paged') > 0 ) {
232 $user_ts_type = 'paged';
234 foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) {
235 $func = 'is_' . $type;
236 if ( call_user_func($func) ) {
237 $user_ts_type = $type;
242 $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
243 } elseif ( is_front_page() ) {
244 $redirect['path'] = trailingslashit($redirect['path']);
247 // Always trailing slash the Front Page URL
248 if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
249 $redirect['path'] = trailingslashit($redirect['path']);
251 // Ignore differences in host capitalization, as this can lead to infinite redirects
252 // Only redirect no-www <=> yes-www
253 if ( strtolower($original['host']) == strtolower($redirect['host']) ||
254 ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
255 $redirect['host'] = $original['host'];
257 $compare_original = array($original['host'], $original['path']);
259 if ( !empty( $original['port'] ) )
260 $compare_original[] = $original['port'];
262 if ( !empty( $original['query'] ) )
263 $compare_original[] = $original['query'];
265 $compare_redirect = array($redirect['host'], $redirect['path']);
267 if ( !empty( $redirect['port'] ) )
268 $compare_redirect[] = $redirect['port'];
270 if ( !empty( $redirect['query'] ) )
271 $compare_redirect[] = $redirect['query'];
273 if ( $compare_original !== $compare_redirect ) {
274 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
275 if ( !empty($redirect['port']) )
276 $redirect_url .= ':' . $redirect['port'];
277 $redirect_url .= $redirect['path'];
278 if ( !empty($redirect['query']) )
279 $redirect_url .= '?' . $redirect['query'];
282 if ( $redirect_url == $requested_url )
285 // Note that you can use the "redirect_canonical" filter to cancel a canonical redirect for whatever reason by returning FALSE
286 $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
288 if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
291 if ( $do_redirect ) {
292 // protect against chained redirects
293 if ( !redirect_canonical($redirect_url, false) ) {
294 wp_redirect($redirect_url, 301);
298 // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
302 return $redirect_url;
307 * Attempts to guess correct post based on query vars.
312 * @return bool|string Returns False, if it can't find post, returns correct
313 * location on success.
315 function redirect_guess_404_permalink() {
318 if ( !get_query_var('name') )
321 $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
323 // if any of year, monthnum, or day are set, use them to refine the query
324 if ( get_query_var('year') )
325 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
326 if ( get_query_var('monthnum') )
327 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
328 if ( get_query_var('day') )
329 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
331 $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
334 return get_permalink($post_id);
337 add_action('template_redirect', 'redirect_canonical');