3 * Canonical API to handle WordPress Redirecting
5 * Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference"
13 * Redirects incoming links to the proper URL based on the site url.
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
17 * prevents penalty for duplicate content by redirecting all incoming links to
20 * Prevents redirection for feeds, trackbacks, searches, comment popup, and
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
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 ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) {
46 // If we're not in wp-admin and the post has been published and preview nonce
47 // is non-existent or invalid then no need for preview in query
48 if ( is_preview() && get_query_var( 'p' ) && 'publish' == get_post_status( get_query_var( 'p' ) ) ) {
49 if ( ! isset( $_GET['preview_id'] )
50 || ! isset( $_GET['preview_nonce'] )
51 || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) {
52 $wp_query->is_preview = false;
56 if ( is_trackback() || is_search() || is_comments_popup() || is_admin() || is_preview() || is_robots() || ( $is_IIS && !iis7_supports_permalinks() ) ) {
60 if ( !$requested_url ) {
61 // build the URL in the address bar
62 $requested_url = is_ssl() ? 'https://' : 'http://';
63 $requested_url .= $_SERVER['HTTP_HOST'];
64 $requested_url .= $_SERVER['REQUEST_URI'];
67 $original = @parse_url($requested_url);
68 if ( false === $original )
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
75 // $original['path'] = preg_replace('|/index\.php$|', '/', $original['path']);
77 $redirect = $original;
78 $redirect_url = false;
81 if ( !isset($redirect['path']) )
82 $redirect['path'] = '';
83 if ( !isset($redirect['query']) )
84 $redirect['query'] = '';
86 // If the original URL ended with non-breaking spaces, they were almost
87 // certainly inserted by accident. Let's remove them, so the reader doesn't
88 // see a 404 error with no obvious cause.
89 $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] );
91 // It's not a preview, so remove it from URL
92 if ( get_query_var( 'preview' ) ) {
93 $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] );
96 if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
97 if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
98 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url );
99 $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH );
103 if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) {
105 $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) );
107 if ( isset($vars[0]) && $vars = $vars[0] ) {
108 if ( 'revision' == $vars->post_type && $vars->post_parent > 0 )
109 $id = $vars->post_parent;
111 if ( $redirect_url = get_permalink($id) )
112 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
116 // These tests give us a WP-generated permalink
119 // 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') );
121 if ( $id && $redirect_post = get_post($id) ) {
122 $post_type_obj = get_post_type_object($redirect_post->post_type);
123 if ( $post_type_obj->public ) {
124 $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 );
129 if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) {
130 $year = get_query_var( 'year' );
131 $month = get_query_var( 'monthnum' );
132 $day = get_query_var( 'day' );
133 $date = sprintf( '%04d-%02d-%02d', $year, $month, $day );
134 if ( ! wp_checkdate( $month, $day, $year, $date ) ) {
135 $redirect_url = get_month_link( $year, $month );
136 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url );
138 } elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) {
139 $redirect_url = get_year_link( get_query_var( 'year' ) );
140 $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url );
143 if ( ! $redirect_url ) {
144 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 );
149 } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
150 // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
151 if ( is_attachment() && !empty($_GET['attachment_id']) && ! $redirect_url ) {
152 if ( $redirect_url = get_attachment_link(get_query_var('attachment_id')) )
153 $redirect['query'] = remove_query_arg('attachment_id', $redirect['query']);
154 } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
155 if ( $redirect_url = get_permalink(get_query_var('p')) )
156 $redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']);
157 } elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) {
158 if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) )
159 $redirect['query'] = remove_query_arg('name', $redirect['query']);
160 } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
161 if ( $redirect_url = get_permalink(get_query_var('page_id')) )
162 $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 ) {
164 $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 ) {
166 if ( $redirect_url = get_permalink(get_option('page_for_posts')) )
167 $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
168 } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
169 $m = get_query_var('m');
170 switch ( strlen($m) ) {
172 $redirect_url = get_year_link($m);
175 $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
178 $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
182 $redirect['query'] = remove_query_arg('m', $redirect['query']);
183 // now moving on to non ?m=X year/month/day links
184 } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) {
185 if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
186 $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
187 } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) {
188 if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
189 $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
190 } elseif ( is_year() && !empty($_GET['year']) ) {
191 if ( $redirect_url = get_year_link(get_query_var('year')) )
192 $redirect['query'] = remove_query_arg('year', $redirect['query']);
193 } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
194 $author = get_userdata(get_query_var('author'));
195 if ( ( false !== $author ) && $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_author = %d AND $wpdb->posts.post_status = 'publish' LIMIT 1", $author->ID ) ) ) {
196 if ( $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) )
197 $redirect['query'] = remove_query_arg('author', $redirect['query']);
199 } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
202 foreach ( $wp_query->tax_query->queried_terms as $tax_query )
203 $term_count += count( $tax_query['terms'] );
205 $obj = $wp_query->get_queried_object();
206 if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {
207 if ( !empty($redirect['query']) ) {
208 // Strip taxonomy query vars off the url.
209 $qv_remove = array( 'term', 'taxonomy');
210 if ( is_category() ) {
211 $qv_remove[] = 'category_name';
212 $qv_remove[] = 'cat';
213 } elseif ( is_tag() ) {
214 $qv_remove[] = 'tag';
215 $qv_remove[] = 'tag_id';
216 } else { // Custom taxonomies will have a custom query var, remove those too:
217 $tax_obj = get_taxonomy( $obj->taxonomy );
218 if ( false !== $tax_obj->query_var )
219 $qv_remove[] = $tax_obj->query_var;
222 $rewrite_vars = array_diff( array_keys($wp_query->query), array_keys($_GET) );
224 if ( !array_diff($rewrite_vars, array_keys($_GET)) ) { // Check to see if all the Query vars are coming from the rewrite, none are set via $_GET
225 $redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's
227 // Create the destination url for this taxonomy
228 $tax_url = parse_url($tax_url);
229 if ( ! empty($tax_url['query']) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..
230 parse_str($tax_url['query'], $query_vars);
231 $redirect['query'] = add_query_arg($query_vars, $redirect['query']);
232 } else { // Taxonomy is accessible via a "pretty-URL"
233 $redirect['path'] = $tax_url['path'];
236 } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite
237 foreach ( $qv_remove as $_qv ) {
238 if ( isset($rewrite_vars[$_qv]) )
239 $redirect['query'] = remove_query_arg($_qv, $redirect['query']);
245 } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
246 $category = get_category_by_path( $cat );
247 $post_terms = wp_get_object_terms($wp_query->get_queried_object_id(), 'category', array('fields' => 'tt_ids'));
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());
253 if ( is_singular() && ! is_front_page() && get_query_var('page') ) {
254 if ( !$redirect_url )
255 $redirect_url = get_permalink( get_queried_object_id() );
256 $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
257 $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
261 if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
262 while ( preg_match( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", $redirect['path'] ) || preg_match( '#/(comments/?)?(feed|rss|rdf|atom|rss2)(/+)?$#', $redirect['path'] ) || preg_match( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+(/+)?$#", $redirect['path'] ) ) {
263 // Strip off paging and feed
264 $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging
265 $redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings
266 $redirect['path'] = preg_replace("#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing comment paging
270 if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) {
271 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
272 if ( !is_singular() && get_query_var( 'withcomments' ) )
273 $addl_path .= 'comments/';
274 if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var('feed') ) || 'rss' == get_query_var('feed') )
275 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' );
277 $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
278 $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
279 } elseif ( is_feed() && 'old' == get_query_var('feed') ) {
280 $old_feed_files = array(
281 'wp-atom.php' => 'atom',
282 'wp-commentsrss2.php' => 'comments_rss2',
283 'wp-feed.php' => get_default_feed(),
284 'wp-rdf.php' => 'rdf',
285 'wp-rss.php' => 'rss2',
286 'wp-rss2.php' => 'rss2',
288 if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) {
289 $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] );
290 wp_redirect( $redirect_url, 301 );
295 if ( get_query_var('paged') > 0 ) {
296 $paged = get_query_var('paged');
297 $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
299 if ( $paged > 1 && !is_single() ) {
300 $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged');
301 } elseif ( !is_single() ) {
302 $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
304 } elseif ( $paged > 1 ) {
305 $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
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 ) ) ) {
310 $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'] );
314 $redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/
315 if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/' . $wp_rewrite->index . '/') === false )
316 $redirect['path'] = trailingslashit($redirect['path']) . $wp_rewrite->index . '/';
317 if ( !empty( $addl_path ) )
318 $redirect['path'] = trailingslashit($redirect['path']) . $addl_path;
319 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
322 if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
323 if ( is_multisite() ) {
324 /** This filter is documented in wp-login.php */
325 $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
327 $redirect_url = site_url( 'wp-login.php?action=register' );
330 wp_redirect( $redirect_url, 301 );
335 // tack on any additional query vars
336 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
337 if ( $redirect_url && !empty($redirect['query']) ) {
338 parse_str( $redirect['query'], $_parsed_query );
339 $redirect = @parse_url($redirect_url);
341 if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
342 parse_str( $redirect['query'], $_parsed_redirect_query );
344 if ( empty( $_parsed_redirect_query['name'] ) )
345 unset( $_parsed_query['name'] );
348 $_parsed_query = rawurlencode_deep( $_parsed_query );
349 $redirect_url = add_query_arg( $_parsed_query, $redirect_url );
353 $redirect = @parse_url($redirect_url);
355 // www.example.com vs example.com
356 $user_home = @parse_url(home_url());
357 if ( !empty($user_home['host']) )
358 $redirect['host'] = $user_home['host'];
359 if ( empty($user_home['path']) )
360 $user_home['path'] = '/';
363 if ( !empty($user_home['port']) )
364 $redirect['port'] = $user_home['port'];
366 unset($redirect['port']);
368 // trailing /index.php
369 $redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']);
371 // Remove trailing spaces from the path
372 $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] );
374 if ( !empty( $redirect['query'] ) ) {
375 // Remove trailing spaces from certain terminating query string args
376 $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] );
378 // Clean up empty query strings
379 $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
381 // Redirect obsolete feeds
382 $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] );
384 // Remove redundant leading ampersands
385 $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
388 // strip /index.php/ when we're not using PATHINFO permalinks
389 if ( !$wp_rewrite->using_index_permalinks() )
390 $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] );
393 if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) {
395 if ( get_query_var('paged') > 0 ) {
396 $user_ts_type = 'paged';
398 foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) {
399 $func = 'is_' . $type;
400 if ( call_user_func($func) ) {
401 $user_ts_type = $type;
406 $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
407 } elseif ( is_front_page() ) {
408 $redirect['path'] = trailingslashit($redirect['path']);
411 // Strip multiple slashes out of the URL
412 if ( strpos($redirect['path'], '//') > -1 )
413 $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']);
415 // Always trailing slash the Front Page URL
416 if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
417 $redirect['path'] = trailingslashit($redirect['path']);
419 // Ignore differences in host capitalization, as this can lead to infinite redirects
420 // Only redirect no-www <=> yes-www
421 if ( strtolower($original['host']) == strtolower($redirect['host']) ||
422 ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
423 $redirect['host'] = $original['host'];
425 $compare_original = array( $original['host'], $original['path'] );
427 if ( !empty( $original['port'] ) )
428 $compare_original[] = $original['port'];
430 if ( !empty( $original['query'] ) )
431 $compare_original[] = $original['query'];
433 $compare_redirect = array( $redirect['host'], $redirect['path'] );
435 if ( !empty( $redirect['port'] ) )
436 $compare_redirect[] = $redirect['port'];
438 if ( !empty( $redirect['query'] ) )
439 $compare_redirect[] = $redirect['query'];
441 if ( $compare_original !== $compare_redirect ) {
442 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
443 if ( !empty($redirect['port']) )
444 $redirect_url .= ':' . $redirect['port'];
445 $redirect_url .= $redirect['path'];
446 if ( !empty($redirect['query']) )
447 $redirect_url .= '?' . $redirect['query'];
450 if ( !$redirect_url || $redirect_url == $requested_url )
453 // Hex encoded octets are case-insensitive.
454 if ( false !== strpos($requested_url, '%') ) {
455 if ( !function_exists('lowercase_octets') ) {
456 function lowercase_octets($matches) {
457 return strtolower( $matches[0] );
460 $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
464 * Filter the canonical redirect URL.
466 * Returning false to this filter will cancel the redirect.
470 * @param string $redirect_url The redirect URL.
471 * @param string $requested_url The requested URL.
473 $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
475 if ( !$redirect_url || $redirect_url == $requested_url ) // yes, again -- in case the filter aborted the request
478 if ( $do_redirect ) {
479 // protect against chained redirects
480 if ( !redirect_canonical($redirect_url, false) ) {
481 wp_redirect($redirect_url, 301);
485 // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
489 return $redirect_url;
494 * Removes arguments from a query string if they are not present in a URL
495 * DO NOT use this in plugin code.
500 * @param string $query_string
501 * @param array $args_to_check
503 * @return string The altered query string
505 function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) {
506 $parsed_url = @parse_url( $url );
507 if ( ! empty( $parsed_url['query'] ) ) {
508 parse_str( $parsed_url['query'], $parsed_query );
509 foreach ( $args_to_check as $qv ) {
510 if ( !isset( $parsed_query[$qv] ) )
511 $query_string = remove_query_arg( $qv, $query_string );
514 $query_string = remove_query_arg( $args_to_check, $query_string );
516 return $query_string;
520 * Attempts to guess the correct URL based on query vars
524 * @global wpdb $wpdb WordPress database abstraction object.
526 * @return bool|string The correct URL if one is found. False on failure.
528 function redirect_guess_404_permalink() {
529 global $wpdb, $wp_rewrite;
531 if ( get_query_var('name') ) {
532 $where = $wpdb->prepare("post_name LIKE %s", $wpdb->esc_like( get_query_var('name') ) . '%');
534 // if any of post_type, year, monthnum, or day are set, use them to refine the query
535 if ( get_query_var('post_type') )
536 $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
538 $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
540 if ( get_query_var('year') )
541 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
542 if ( get_query_var('monthnum') )
543 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
544 if ( get_query_var('day') )
545 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
547 $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
550 if ( get_query_var( 'feed' ) )
551 return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
552 elseif ( get_query_var( 'page' ) )
553 return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
555 return get_permalink( $post_id );
561 function wp_redirect_admin_locations() {
563 if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
567 home_url( 'wp-admin', 'relative' ),
568 home_url( 'dashboard', 'relative' ),
569 home_url( 'admin', 'relative' ),
570 site_url( 'dashboard', 'relative' ),
571 site_url( 'admin', 'relative' ),
573 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
574 wp_redirect( admin_url() );
579 home_url( 'wp-login.php', 'relative' ),
580 home_url( 'login', 'relative' ),
581 site_url( 'login', 'relative' ),
583 if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
584 wp_redirect( site_url( 'wp-login.php', 'login' ) );