redirect_canonical

Redirects incoming links to the proper URL based on the site url.

Description

(string|void) redirect_canonical( (constant) $requested_url = null, (bool) $do_redirect = true ); 

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 penalty for duplicate content by redirecting all incoming links to one or the other.

Prevents redirection for feeds, trackbacks, searches, and admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+, page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST requests.

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.

Returns (string|void)

The string of the URL, if redirect needed.

Parameters (2)

0. $requested_url — Optional. (constant) => null
The URL that was requested, used to figure if redirect is needed.
1. $do_redirect — Optional. (bool) => true
Redirect to the new URL.

Usage

  1. if ( !function_exists( 'redirect_canonical' ) ) { 
  2. require_once ABSPATH . WPINC . '/canonical.php'; 
  3.  
  4. // Optional. The URL that was requested, used to 
  5. // figure if redirect is needed. 
  6. $requested_url = null; 
  7.  
  8. // Optional. Redirect to the new URL. 
  9. $do_redirect = true; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = redirect_canonical($requested_url, $do_redirect); 
  13.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/canonical.php  
  1. function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
  2. global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp; 
  3.  
  4. if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) { 
  5. return; 
  6.  
  7. // If we're not in wp-admin and the post has been published and preview nonce 
  8. // is non-existent or invalid then no need for preview in query 
  9. if ( is_preview() && get_query_var( 'p' ) && 'publish' == get_post_status( get_query_var( 'p' ) ) ) { 
  10. if ( ! isset( $_GET['preview_id'] ) 
  11. || ! isset( $_GET['preview_nonce'] ) 
  12. || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) { 
  13. $wp_query->is_preview = false; 
  14.  
  15. if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && !iis7_supports_permalinks() ) ) { 
  16. return; 
  17.  
  18. if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) { 
  19. // build the URL in the address bar 
  20. $requested_url = is_ssl() ? 'https://' : 'http://'; 
  21. $requested_url .= $_SERVER['HTTP_HOST']; 
  22. $requested_url .= $_SERVER['REQUEST_URI']; 
  23.  
  24. $original = @parse_url($requested_url); 
  25. if ( false === $original ) { 
  26. return; 
  27.  
  28. $redirect = $original; 
  29. $redirect_url = false; 
  30.  
  31. // Notice fixing 
  32. if ( !isset($redirect['path']) ) 
  33. $redirect['path'] = ''; 
  34. if ( !isset($redirect['query']) ) 
  35. $redirect['query'] = ''; 
  36.  
  37. // If the original URL ended with non-breaking spaces, they were almost 
  38. // certainly inserted by accident. Let's remove them, so the reader doesn't 
  39. // see a 404errorwith no obvious cause. 
  40. $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] ); 
  41.  
  42. // It's not a preview, so remove it from URL 
  43. if ( get_query_var( 'preview' ) ) { 
  44. $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] ); 
  45.  
  46. if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) { 
  47. if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) { 
  48. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url ); 
  49. $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH ); 
  50.  
  51. if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) { 
  52.  
  53. $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) ); 
  54.  
  55. if ( isset($vars[0]) && $vars = $vars[0] ) { 
  56. if ( 'revision' == $vars->post_type && $vars->post_parent > 0 ) 
  57. $id = $vars->post_parent; 
  58.  
  59. if ( $redirect_url = get_permalink($id) ) 
  60. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); 
  61.  
  62. // These tests give us a WP-generated permalink 
  63. if ( is_404() ) { 
  64.  
  65. // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's 
  66. $id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') ); 
  67. if ( $id && $redirect_post = get_post($id) ) { 
  68. $post_type_obj = get_post_type_object($redirect_post->post_type); 
  69. if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) { 
  70. $redirect_url = get_permalink($redirect_post); 
  71. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url ); 
  72.  
  73. if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) { 
  74. $year = get_query_var( 'year' ); 
  75. $month = get_query_var( 'monthnum' ); 
  76. $day = get_query_var( 'day' ); 
  77. $date = sprintf( '%04d-%02d-%02d', $year, $month, $day ); 
  78. if ( ! wp_checkdate( $month, $day, $year, $date ) ) { 
  79. $redirect_url = get_month_link( $year, $month ); 
  80. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url ); 
  81. } elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) { 
  82. $redirect_url = get_year_link( get_query_var( 'year' ) ); 
  83. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url ); 
  84.  
  85. if ( ! $redirect_url ) { 
  86. if ( $redirect_url = redirect_guess_404_permalink() ) { 
  87. $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 ); 
  88.  
  89. if ( get_query_var( 'page' ) && $wp_query->post && 
  90. false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) { 
  91. $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' ); 
  92. $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); 
  93. $redirect_url = get_permalink( $wp_query->post->ID ); 
  94.  
  95. } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) { 
  96. // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101 
  97. if ( is_attachment() && 
  98. ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) && 
  99. ! $redirect_url ) { 
  100. if ( ! empty( $_GET['attachment_id'] ) ) { 
  101. $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) ); 
  102. if ( $redirect_url ) { 
  103. $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] ); 
  104. } else { 
  105. $redirect_url = get_attachment_link(); 
  106. } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) { 
  107. if ( $redirect_url = get_permalink(get_query_var('p')) ) 
  108. $redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']); 
  109. } elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) { 
  110. if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) ) 
  111. $redirect['query'] = remove_query_arg('name', $redirect['query']); 
  112. } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) { 
  113. if ( $redirect_url = get_permalink(get_query_var('page_id')) ) 
  114. $redirect['query'] = remove_query_arg('page_id', $redirect['query']); 
  115. } elseif ( is_page() && !is_feed() && 'page' == get_option('show_on_front') && get_queried_object_id() == get_option('page_on_front') && ! $redirect_url ) { 
  116. $redirect_url = home_url('/'); 
  117. } 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 ) { 
  118. if ( $redirect_url = get_permalink(get_option('page_for_posts')) ) 
  119. $redirect['query'] = remove_query_arg('page_id', $redirect['query']); 
  120. } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) { 
  121. $m = get_query_var('m'); 
  122. switch ( strlen($m) ) { 
  123. case 4: // Yearly 
  124. $redirect_url = get_year_link($m); 
  125. break; 
  126. case 6: // Monthly 
  127. $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) ); 
  128. break; 
  129. case 8: // Daily 
  130. $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2)); 
  131. break; 
  132. if ( $redirect_url ) 
  133. $redirect['query'] = remove_query_arg('m', $redirect['query']); 
  134. // now moving on to non ?m=X year/month/day links 
  135. } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) { 
  136. if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) ) 
  137. $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']); 
  138. } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) { 
  139. if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) ) 
  140. $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']); 
  141. } elseif ( is_year() && !empty($_GET['year']) ) { 
  142. if ( $redirect_url = get_year_link(get_query_var('year')) ) 
  143. $redirect['query'] = remove_query_arg('year', $redirect['query']); 
  144. } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) { 
  145. $author = get_userdata(get_query_var('author')); 
  146. 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 ) ) ) { 
  147. if ( $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) ) 
  148. $redirect['query'] = remove_query_arg('author', $redirect['query']); 
  149. } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories) 
  150.  
  151. $term_count = 0; 
  152. foreach ( $wp_query->tax_query->queried_terms as $tax_query ) 
  153. $term_count += count( $tax_query['terms'] ); 
  154.  
  155. $obj = $wp_query->get_queried_object(); 
  156. if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) { 
  157. if ( !empty($redirect['query']) ) { 
  158. // Strip taxonomy query vars off the url. 
  159. $qv_remove = array( 'term', 'taxonomy'); 
  160. if ( is_category() ) { 
  161. $qv_remove[] = 'category_name'; 
  162. $qv_remove[] = 'cat'; 
  163. } elseif ( is_tag() ) { 
  164. $qv_remove[] = 'tag'; 
  165. $qv_remove[] = 'tag_id'; 
  166. } else { // Custom taxonomies will have a custom query var, remove those too: 
  167. $tax_obj = get_taxonomy( $obj->taxonomy ); 
  168. if ( false !== $tax_obj->query_var ) 
  169. $qv_remove[] = $tax_obj->query_var; 
  170.  
  171. $rewrite_vars = array_diff( array_keys($wp_query->query), array_keys($_GET) ); 
  172.  
  173. 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 
  174. $redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's 
  175.  
  176. // Create the destination url for this taxonomy 
  177. $tax_url = parse_url($tax_url); 
  178. if ( ! empty($tax_url['query']) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv.. 
  179. parse_str($tax_url['query'], $query_vars); 
  180. $redirect['query'] = add_query_arg($query_vars, $redirect['query']); 
  181. } else { // Taxonomy is accessible via a "pretty-URL" 
  182. $redirect['path'] = $tax_url['path']; 
  183.  
  184. } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite 
  185. foreach ( $qv_remove as $_qv ) { 
  186. if ( isset($rewrite_vars[$_qv]) ) 
  187. $redirect['query'] = remove_query_arg($_qv, $redirect['query']); 
  188.  
  189. } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) { 
  190. $category = get_category_by_path( $cat ); 
  191. if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) { 
  192. $redirect_url = get_permalink($wp_query->get_queried_object_id()); 
  193.  
  194. // Post Paging 
  195. if ( is_singular() && get_query_var('page') ) { 
  196. if ( !$redirect_url ) 
  197. $redirect_url = get_permalink( get_queried_object_id() ); 
  198.  
  199. $page = get_query_var( 'page' ); 
  200. if ( $page > 1 ) { 
  201. if ( is_front_page() ) { 
  202. $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' ); 
  203. } else { 
  204. $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' ); 
  205. $redirect['query'] = remove_query_arg( 'page', $redirect['query'] ); 
  206.  
  207. // paging and feeds 
  208. if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) { 
  209. 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'] ) ) { 
  210. // Strip off paging and feed 
  211. $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging 
  212. $redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings 
  213. $redirect['path'] = preg_replace("#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing comment paging 
  214.  
  215. $addl_path = ''; 
  216. if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) { 
  217. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; 
  218. if ( !is_singular() && get_query_var( 'withcomments' ) ) 
  219. $addl_path .= 'comments/'; 
  220. if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var('feed') ) || 'rss' == get_query_var('feed') ) 
  221. $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' ); 
  222. else 
  223. $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' ); 
  224. $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] ); 
  225. } elseif ( is_feed() && 'old' == get_query_var('feed') ) { 
  226. $old_feed_files = array( 
  227. 'wp-atom.php' => 'atom',  
  228. 'wp-commentsrss2.php' => 'comments_rss2',  
  229. 'wp-feed.php' => get_default_feed(),  
  230. 'wp-rdf.php' => 'rdf',  
  231. 'wp-rss.php' => 'rss2',  
  232. 'wp-rss2.php' => 'rss2',  
  233. ); 
  234. if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) { 
  235. $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] ); 
  236. wp_redirect( $redirect_url, 301 ); 
  237. die(); 
  238.  
  239. if ( get_query_var('paged') > 0 ) { 
  240. $paged = get_query_var('paged'); 
  241. $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); 
  242. if ( !is_feed() ) { 
  243. if ( $paged > 1 && !is_single() ) { 
  244. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged'); 
  245. } elseif ( !is_single() ) { 
  246. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : ''; 
  247. } elseif ( $paged > 1 ) { 
  248. $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] ); 
  249.  
  250. if ( get_option( 'page_comments' ) && ( 
  251. ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) || 
  252. ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 ) 
  253. ) ) { 
  254. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' ); 
  255. $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] ); 
  256.  
  257. $redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/ 
  258. if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/' . $wp_rewrite->index . '/') === false ) 
  259. $redirect['path'] = trailingslashit($redirect['path']) . $wp_rewrite->index . '/'; 
  260. if ( !empty( $addl_path ) ) 
  261. $redirect['path'] = trailingslashit($redirect['path']) . $addl_path; 
  262. $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; 
  263.  
  264. if ( 'wp-register.php' == basename( $redirect['path'] ) ) { 
  265. if ( is_multisite() ) { 
  266. /** This filter is documented in wp-login.php */ 
  267. $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) ); 
  268. } else { 
  269. $redirect_url = wp_registration_url(); 
  270.  
  271. wp_redirect( $redirect_url, 301 ); 
  272. die(); 
  273.  
  274. // tack on any additional query vars 
  275. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 
  276. if ( $redirect_url && !empty($redirect['query']) ) { 
  277. parse_str( $redirect['query'], $_parsed_query ); 
  278. $redirect = @parse_url($redirect_url); 
  279.  
  280. if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) { 
  281. parse_str( $redirect['query'], $_parsed_redirect_query ); 
  282.  
  283. if ( empty( $_parsed_redirect_query['name'] ) ) 
  284. unset( $_parsed_query['name'] ); 
  285.  
  286. $_parsed_query = rawurlencode_deep( $_parsed_query ); 
  287. $redirect_url = add_query_arg( $_parsed_query, $redirect_url ); 
  288.  
  289. if ( $redirect_url ) 
  290. $redirect = @parse_url($redirect_url); 
  291.  
  292. // www.example.com vs example.com 
  293. $user_home = @parse_url(home_url()); 
  294. if ( !empty($user_home['host']) ) 
  295. $redirect['host'] = $user_home['host']; 
  296. if ( empty($user_home['path']) ) 
  297. $user_home['path'] = '/'; 
  298.  
  299. // Handle ports 
  300. if ( !empty($user_home['port']) ) 
  301. $redirect['port'] = $user_home['port']; 
  302. else 
  303. unset($redirect['port']); 
  304.  
  305. // trailing /index.php 
  306. $redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']); 
  307.   
  308. // Remove trailing spaces from the path 
  309. $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] ); 
  310.   
  311. if ( !empty( $redirect['query'] ) ) { 
  312. // Remove trailing spaces from certain terminating query string args 
  313. $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] ); 
  314.   
  315. // Clean up empty query strings 
  316. $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&'); 
  317.   
  318. // Redirect obsolete feeds 
  319. $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] ); 
  320.   
  321. // Remove redundant leading ampersands 
  322. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); 
  323.   
  324. // strip /index.php/ when we're not using PATHINFO permalinks 
  325. if ( !$wp_rewrite->using_index_permalinks() ) 
  326. $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] ); 
  327.   
  328. // trailing slashes 
  329. if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) { 
  330. $user_ts_type = ''; 
  331. if ( get_query_var('paged') > 0 ) { 
  332. $user_ts_type = 'paged'; 
  333. } else { 
  334. foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) { 
  335. $func = 'is_' . $type; 
  336. if ( call_user_func($func) ) { 
  337. $user_ts_type = $type; 
  338. break; 
  339. $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type); 
  340. } elseif ( is_front_page() ) { 
  341. $redirect['path'] = trailingslashit($redirect['path']); 
  342.   
  343. // Strip multiple slashes out of the URL 
  344. if ( strpos($redirect['path'], '//') > -1 ) 
  345. $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']); 
  346.   
  347. // Always trailing slash the Front Page URL 
  348. if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) ) 
  349. $redirect['path'] = trailingslashit($redirect['path']); 
  350.   
  351. // Ignore differences in host capitalization, as this can lead to infinite redirects 
  352. // Only redirect no-www <=> yes-www 
  353. if ( strtolower($original['host']) == strtolower($redirect['host']) || 
  354. ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) ) 
  355. $redirect['host'] = $original['host']; 
  356.   
  357. $compare_original = array( $original['host'], $original['path'] ); 
  358.   
  359. if ( !empty( $original['port'] ) ) 
  360. $compare_original[] = $original['port']; 
  361.   
  362. if ( !empty( $original['query'] ) ) 
  363. $compare_original[] = $original['query']; 
  364.   
  365. $compare_redirect = array( $redirect['host'], $redirect['path'] ); 
  366.   
  367. if ( !empty( $redirect['port'] ) ) 
  368. $compare_redirect[] = $redirect['port']; 
  369.   
  370. if ( !empty( $redirect['query'] ) ) 
  371. $compare_redirect[] = $redirect['query']; 
  372.   
  373. if ( $compare_original !== $compare_redirect ) { 
  374. $redirect_url = $redirect['scheme'] . '://' . $redirect['host']; 
  375. if ( !empty($redirect['port']) ) 
  376. $redirect_url .= ':' . $redirect['port']; 
  377. $redirect_url .= $redirect['path']; 
  378. if ( !empty($redirect['query']) ) 
  379. $redirect_url .= '?' . $redirect['query']; 
  380.   
  381. if ( ! $redirect_url || $redirect_url == $requested_url ) { 
  382. return; 
  383.   
  384. // Hex encoded octets are case-insensitive. 
  385. if ( false !== strpos($requested_url, '%') ) { 
  386. if ( !function_exists('lowercase_octets') ) { 
  387. /** 
  388. * Converts the first hex-encoded octet match to lowercase. 
  389. * @since 3.1.0 
  390. * @ignore 
  391. * @param array $matches Hex-encoded octet matches for the requested URL. 
  392. * @return string Lowercased version of the first match. 
  393. */ 
  394. function lowercase_octets($matches) { 
  395. return strtolower( $matches[0] ); 
  396. $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url); 
  397.  
  398. /** 
  399. * Filters the canonical redirect URL. 
  400. * Returning false to this filter will cancel the redirect. 
  401. * @since 2.3.0 
  402. * @param string $redirect_url The redirect URL. 
  403. * @param string $requested_url The requested URL. 
  404. */ 
  405. $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url ); 
  406.  
  407. // yes, again -- in case the filter aborted the request 
  408. if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) { 
  409. return; 
  410.  
  411. if ( $do_redirect ) { 
  412. // protect against chained redirects 
  413. if ( !redirect_canonical($redirect_url, false) ) { 
  414. wp_redirect($redirect_url, 301); 
  415. exit(); 
  416. } else { 
  417. // Debug 
  418. // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) ); 
  419. return; 
  420. } else { 
  421. return $redirect_url;