You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

658 lines
26 KiB

  1. <?php
  2. /**
  3. * Canonical API to handle WordPress Redirecting
  4. *
  5. * Based on "Permalink Redirect" from Scott Yang and "Enforce www. Preference"
  6. * by Mark Jaquith
  7. *
  8. * @package WordPress
  9. * @since 2.3.0
  10. */
  11. /**
  12. * Redirects incoming links to the proper URL based on the site url.
  13. *
  14. * Search engines consider www.somedomain.com and somedomain.com to be two
  15. * different URLs when they both go to the same location. This SEO enhancement
  16. * prevents penalty for duplicate content by redirecting all incoming links to
  17. * one or the other.
  18. *
  19. * Prevents redirection for feeds, trackbacks, searches, and
  20. * admin URLs. Does not redirect on non-pretty-permalink-supporting IIS 7+,
  21. * page/post previews, WP admin, Trackbacks, robots.txt, searches, or on POST
  22. * requests.
  23. *
  24. * Will also attempt to find the correct link when a user enters a URL that does
  25. * not exist based on exact WordPress query. Will instead try to parse the URL
  26. * or query in an attempt to figure the correct page to go to.
  27. *
  28. * @since 2.3.0
  29. *
  30. * @global WP_Rewrite $wp_rewrite
  31. * @global bool $is_IIS
  32. * @global WP_Query $wp_query
  33. * @global wpdb $wpdb WordPress database abstraction object.
  34. *
  35. * @param string $requested_url Optional. The URL that was requested, used to
  36. * figure if redirect is needed.
  37. * @param bool $do_redirect Optional. Redirect to the new URL.
  38. * @return string|void The string of the URL, if redirect needed.
  39. */
  40. function redirect_canonical( $requested_url = null, $do_redirect = true ) {
  41. global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp;
  42. if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ) ) ) {
  43. return;
  44. }
  45. // If we're not in wp-admin and the post has been published and preview nonce
  46. // is non-existent or invalid then no need for preview in query
  47. if ( is_preview() && get_query_var( 'p' ) && 'publish' == get_post_status( get_query_var( 'p' ) ) ) {
  48. if ( ! isset( $_GET['preview_id'] )
  49. || ! isset( $_GET['preview_nonce'] )
  50. || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] ) ) {
  51. $wp_query->is_preview = false;
  52. }
  53. }
  54. if ( is_trackback() || is_search() || is_admin() || is_preview() || is_robots() || ( $is_IIS && !iis7_supports_permalinks() ) ) {
  55. return;
  56. }
  57. if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
  58. // build the URL in the address bar
  59. $requested_url = is_ssl() ? 'https://' : 'http://';
  60. $requested_url .= $_SERVER['HTTP_HOST'];
  61. $requested_url .= $_SERVER['REQUEST_URI'];
  62. }
  63. $original = @parse_url($requested_url);
  64. if ( false === $original ) {
  65. return;
  66. }
  67. $redirect = $original;
  68. $redirect_url = false;
  69. // Notice fixing
  70. if ( !isset($redirect['path']) )
  71. $redirect['path'] = '';
  72. if ( !isset($redirect['query']) )
  73. $redirect['query'] = '';
  74. // If the original URL ended with non-breaking spaces, they were almost
  75. // certainly inserted by accident. Let's remove them, so the reader doesn't
  76. // see a 404 error with no obvious cause.
  77. $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] );
  78. // It's not a preview, so remove it from URL
  79. if ( get_query_var( 'preview' ) ) {
  80. $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] );
  81. }
  82. if ( is_feed() && ( $id = get_query_var( 'p' ) ) ) {
  83. if ( $redirect_url = get_post_comments_feed_link( $id, get_query_var( 'feed' ) ) ) {
  84. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed'), $redirect_url );
  85. $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH );
  86. }
  87. }
  88. if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) {
  89. $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) );
  90. if ( isset($vars[0]) && $vars = $vars[0] ) {
  91. if ( 'revision' == $vars->post_type && $vars->post_parent > 0 )
  92. $id = $vars->post_parent;
  93. if ( $redirect_url = get_permalink($id) )
  94. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
  95. }
  96. }
  97. // These tests give us a WP-generated permalink
  98. if ( is_404() ) {
  99. // Redirect ?page_id, ?p=, ?attachment_id= to their respective url's
  100. $id = max( get_query_var('p'), get_query_var('page_id'), get_query_var('attachment_id') );
  101. if ( $id && $redirect_post = get_post($id) ) {
  102. $post_type_obj = get_post_type_object($redirect_post->post_type);
  103. if ( $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) {
  104. $redirect_url = get_permalink($redirect_post);
  105. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ), $redirect_url );
  106. }
  107. }
  108. if ( get_query_var( 'day' ) && get_query_var( 'monthnum' ) && get_query_var( 'year' ) ) {
  109. $year = get_query_var( 'year' );
  110. $month = get_query_var( 'monthnum' );
  111. $day = get_query_var( 'day' );
  112. $date = sprintf( '%04d-%02d-%02d', $year, $month, $day );
  113. if ( ! wp_checkdate( $month, $day, $year, $date ) ) {
  114. $redirect_url = get_month_link( $year, $month );
  115. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum', 'day' ), $redirect_url );
  116. }
  117. } elseif ( get_query_var( 'monthnum' ) && get_query_var( 'year' ) && 12 < get_query_var( 'monthnum' ) ) {
  118. $redirect_url = get_year_link( get_query_var( 'year' ) );
  119. $redirect['query'] = _remove_qs_args_if_not_in_url( $redirect['query'], array( 'year', 'monthnum' ), $redirect_url );
  120. }
  121. if ( ! $redirect_url ) {
  122. if ( $redirect_url = redirect_guess_404_permalink() ) {
  123. $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 );
  124. }
  125. }
  126. if ( get_query_var( 'page' ) && $wp_query->post &&
  127. false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) {
  128. $redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
  129. $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
  130. $redirect_url = get_permalink( $wp_query->post->ID );
  131. }
  132. } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
  133. // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
  134. if ( is_attachment() &&
  135. ! array_diff( array_keys( $wp->query_vars ), array( 'attachment', 'attachment_id' ) ) &&
  136. ! $redirect_url ) {
  137. if ( ! empty( $_GET['attachment_id'] ) ) {
  138. $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
  139. if ( $redirect_url ) {
  140. $redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] );
  141. }
  142. } else {
  143. $redirect_url = get_attachment_link();
  144. }
  145. } elseif ( is_single() && !empty($_GET['p']) && ! $redirect_url ) {
  146. if ( $redirect_url = get_permalink(get_query_var('p')) )
  147. $redirect['query'] = remove_query_arg(array('p', 'post_type'), $redirect['query']);
  148. } elseif ( is_single() && !empty($_GET['name']) && ! $redirect_url ) {
  149. if ( $redirect_url = get_permalink( $wp_query->get_queried_object_id() ) )
  150. $redirect['query'] = remove_query_arg('name', $redirect['query']);
  151. } elseif ( is_page() && !empty($_GET['page_id']) && ! $redirect_url ) {
  152. if ( $redirect_url = get_permalink(get_query_var('page_id')) )
  153. $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
  154. } elseif ( is_page() && !is_feed() && 'page' == get_option('show_on_front') && get_queried_object_id() == get_option('page_on_front') && ! $redirect_url ) {
  155. $redirect_url = home_url('/');
  156. } 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 ) {
  157. if ( $redirect_url = get_permalink(get_option('page_for_posts')) )
  158. $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
  159. } elseif ( !empty($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
  160. $m = get_query_var('m');
  161. switch ( strlen($m) ) {
  162. case 4: // Yearly
  163. $redirect_url = get_year_link($m);
  164. break;
  165. case 6: // Monthly
  166. $redirect_url = get_month_link( substr($m, 0, 4), substr($m, 4, 2) );
  167. break;
  168. case 8: // Daily
  169. $redirect_url = get_day_link(substr($m, 0, 4), substr($m, 4, 2), substr($m, 6, 2));
  170. break;
  171. }
  172. if ( $redirect_url )
  173. $redirect['query'] = remove_query_arg('m', $redirect['query']);
  174. // now moving on to non ?m=X year/month/day links
  175. } elseif ( is_day() && get_query_var('year') && get_query_var('monthnum') && !empty($_GET['day']) ) {
  176. if ( $redirect_url = get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')) )
  177. $redirect['query'] = remove_query_arg(array('year', 'monthnum', 'day'), $redirect['query']);
  178. } elseif ( is_month() && get_query_var('year') && !empty($_GET['monthnum']) ) {
  179. if ( $redirect_url = get_month_link(get_query_var('year'), get_query_var('monthnum')) )
  180. $redirect['query'] = remove_query_arg(array('year', 'monthnum'), $redirect['query']);
  181. } elseif ( is_year() && !empty($_GET['year']) ) {
  182. if ( $redirect_url = get_year_link(get_query_var('year')) )
  183. $redirect['query'] = remove_query_arg('year', $redirect['query']);
  184. } elseif ( is_author() && !empty($_GET['author']) && preg_match( '|^[0-9]+$|', $_GET['author'] ) ) {
  185. $author = get_userdata(get_query_var('author'));
  186. 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 ) ) ) {
  187. if ( $redirect_url = get_author_posts_url($author->ID, $author->user_nicename) )
  188. $redirect['query'] = remove_query_arg('author', $redirect['query']);
  189. }
  190. } elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
  191. $term_count = 0;
  192. foreach ( $wp_query->tax_query->queried_terms as $tax_query )
  193. $term_count += count( $tax_query['terms'] );
  194. $obj = $wp_query->get_queried_object();
  195. if ( $term_count <= 1 && !empty($obj->term_id) && ( $tax_url = get_term_link((int)$obj->term_id, $obj->taxonomy) ) && !is_wp_error($tax_url) ) {
  196. if ( !empty($redirect['query']) ) {
  197. // Strip taxonomy query vars off the url.
  198. $qv_remove = array( 'term', 'taxonomy');
  199. if ( is_category() ) {
  200. $qv_remove[] = 'category_name';
  201. $qv_remove[] = 'cat';
  202. } elseif ( is_tag() ) {
  203. $qv_remove[] = 'tag';
  204. $qv_remove[] = 'tag_id';
  205. } else { // Custom taxonomies will have a custom query var, remove those too:
  206. $tax_obj = get_taxonomy( $obj->taxonomy );
  207. if ( false !== $tax_obj->query_var )
  208. $qv_remove[] = $tax_obj->query_var;
  209. }
  210. $rewrite_vars = array_diff( array_keys($wp_query->query), array_keys($_GET) );
  211. 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
  212. $redirect['query'] = remove_query_arg($qv_remove, $redirect['query']); //Remove all of the per-tax qv's
  213. // Create the destination url for this taxonomy
  214. $tax_url = parse_url($tax_url);
  215. if ( ! empty($tax_url['query']) ) { // Taxonomy accessible via ?taxonomy=..&term=.. or any custom qv..
  216. parse_str($tax_url['query'], $query_vars);
  217. $redirect['query'] = add_query_arg($query_vars, $redirect['query']);
  218. } else { // Taxonomy is accessible via a "pretty-URL"
  219. $redirect['path'] = $tax_url['path'];
  220. }
  221. } else { // Some query vars are set via $_GET. Unset those from $_GET that exist via the rewrite
  222. foreach ( $qv_remove as $_qv ) {
  223. if ( isset($rewrite_vars[$_qv]) )
  224. $redirect['query'] = remove_query_arg($_qv, $redirect['query']);
  225. }
  226. }
  227. }
  228. }
  229. } elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
  230. $category = get_category_by_path( $cat );
  231. if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) {
  232. $redirect_url = get_permalink($wp_query->get_queried_object_id());
  233. }
  234. }
  235. // Post Paging
  236. if ( is_singular() && get_query_var('page') ) {
  237. if ( !$redirect_url )
  238. $redirect_url = get_permalink( get_queried_object_id() );
  239. $page = get_query_var( 'page' );
  240. if ( $page > 1 ) {
  241. if ( is_front_page() ) {
  242. $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( "$wp_rewrite->pagination_base/$page", 'paged' );
  243. } else {
  244. $redirect_url = trailingslashit( $redirect_url ) . user_trailingslashit( $page, 'single_paged' );
  245. }
  246. }
  247. $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
  248. }
  249. // paging and feeds
  250. if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
  251. 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'] ) ) {
  252. // Strip off paging and feed
  253. $redirect['path'] = preg_replace("#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing paging
  254. $redirect['path'] = preg_replace('#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path']); // strip off feed endings
  255. $redirect['path'] = preg_replace("#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path']); // strip off any existing comment paging
  256. }
  257. $addl_path = '';
  258. if ( is_feed() && in_array( get_query_var('feed'), $wp_rewrite->feeds ) ) {
  259. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
  260. if ( !is_singular() && get_query_var( 'withcomments' ) )
  261. $addl_path .= 'comments/';
  262. if ( ( 'rss' == get_default_feed() && 'feed' == get_query_var('feed') ) || 'rss' == get_query_var('feed') )
  263. $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == 'rss2' ) ? '' : 'rss2' ), 'feed' );
  264. else
  265. $addl_path .= user_trailingslashit( 'feed/' . ( ( get_default_feed() == get_query_var('feed') || 'feed' == get_query_var('feed') ) ? '' : get_query_var('feed') ), 'feed' );
  266. $redirect['query'] = remove_query_arg( 'feed', $redirect['query'] );
  267. } elseif ( is_feed() && 'old' == get_query_var('feed') ) {
  268. $old_feed_files = array(
  269. 'wp-atom.php' => 'atom',
  270. 'wp-commentsrss2.php' => 'comments_rss2',
  271. 'wp-feed.php' => get_default_feed(),
  272. 'wp-rdf.php' => 'rdf',
  273. 'wp-rss.php' => 'rss2',
  274. 'wp-rss2.php' => 'rss2',
  275. );
  276. if ( isset( $old_feed_files[ basename( $redirect['path'] ) ] ) ) {
  277. $redirect_url = get_feed_link( $old_feed_files[ basename( $redirect['path'] ) ] );
  278. wp_redirect( $redirect_url, 301 );
  279. die();
  280. }
  281. }
  282. if ( get_query_var('paged') > 0 ) {
  283. $paged = get_query_var('paged');
  284. $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] );
  285. if ( !is_feed() ) {
  286. if ( $paged > 1 && !is_single() ) {
  287. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit("$wp_rewrite->pagination_base/$paged", 'paged');
  288. } elseif ( !is_single() ) {
  289. $addl_path = !empty( $addl_path ) ? trailingslashit($addl_path) : '';
  290. }
  291. } elseif ( $paged > 1 ) {
  292. $redirect['query'] = add_query_arg( 'paged', $paged, $redirect['query'] );
  293. }
  294. }
  295. if ( get_option( 'page_comments' ) && (
  296. ( 'newest' == get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 0 ) ||
  297. ( 'newest' != get_option( 'default_comments_page' ) && get_query_var( 'cpage' ) > 1 )
  298. ) ) {
  299. $addl_path = ( !empty( $addl_path ) ? trailingslashit($addl_path) : '' ) . user_trailingslashit( $wp_rewrite->comments_pagination_base . '-' . get_query_var('cpage'), 'commentpaged' );
  300. $redirect['query'] = remove_query_arg( 'cpage', $redirect['query'] );
  301. }
  302. $redirect['path'] = user_trailingslashit( preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/?$|', '/', $redirect['path']) ); // strip off trailing /index.php/
  303. if ( !empty( $addl_path ) && $wp_rewrite->using_index_permalinks() && strpos($redirect['path'], '/' . $wp_rewrite->index . '/') === false )
  304. $redirect['path'] = trailingslashit($redirect['path']) . $wp_rewrite->index . '/';
  305. if ( !empty( $addl_path ) )
  306. $redirect['path'] = trailingslashit($redirect['path']) . $addl_path;
  307. $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path'];
  308. }
  309. if ( 'wp-register.php' == basename( $redirect['path'] ) ) {
  310. if ( is_multisite() ) {
  311. /** This filter is documented in wp-login.php */
  312. $redirect_url = apply_filters( 'wp_signup_location', network_site_url( 'wp-signup.php' ) );
  313. } else {
  314. $redirect_url = wp_registration_url();
  315. }
  316. wp_redirect( $redirect_url, 301 );
  317. die();
  318. }
  319. }
  320. // tack on any additional query vars
  321. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
  322. if ( $redirect_url && !empty($redirect['query']) ) {
  323. parse_str( $redirect['query'], $_parsed_query );
  324. $redirect = @parse_url($redirect_url);
  325. if ( ! empty( $_parsed_query['name'] ) && ! empty( $redirect['query'] ) ) {
  326. parse_str( $redirect['query'], $_parsed_redirect_query );
  327. if ( empty( $_parsed_redirect_query['name'] ) )
  328. unset( $_parsed_query['name'] );
  329. }
  330. $_parsed_query = rawurlencode_deep( $_parsed_query );
  331. $redirect_url = add_query_arg( $_parsed_query, $redirect_url );
  332. }
  333. if ( $redirect_url )
  334. $redirect = @parse_url($redirect_url);
  335. // www.example.com vs example.com
  336. $user_home = @parse_url(home_url());
  337. if ( !empty($user_home['host']) )
  338. $redirect['host'] = $user_home['host'];
  339. if ( empty($user_home['path']) )
  340. $user_home['path'] = '/';
  341. // Handle ports
  342. if ( !empty($user_home['port']) )
  343. $redirect['port'] = $user_home['port'];
  344. else
  345. unset($redirect['port']);
  346. // trailing /index.php
  347. $redirect['path'] = preg_replace('|/' . preg_quote( $wp_rewrite->index, '|' ) . '/*?$|', '/', $redirect['path']);
  348. // Remove trailing spaces from the path
  349. $redirect['path'] = preg_replace( '#(%20| )+$#', '', $redirect['path'] );
  350. if ( !empty( $redirect['query'] ) ) {
  351. // Remove trailing spaces from certain terminating query string args
  352. $redirect['query'] = preg_replace( '#((p|page_id|cat|tag)=[^&]*?)(%20| )+$#', '$1', $redirect['query'] );
  353. // Clean up empty query strings
  354. $redirect['query'] = trim(preg_replace( '#(^|&)(p|page_id|cat|tag)=?(&|$)#', '&', $redirect['query']), '&');
  355. // Redirect obsolete feeds
  356. $redirect['query'] = preg_replace( '#(^|&)feed=rss(&|$)#', '$1feed=rss2$2', $redirect['query'] );
  357. // Remove redundant leading ampersands
  358. $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] );
  359. }
  360. // strip /index.php/ when we're not using PATHINFO permalinks
  361. if ( !$wp_rewrite->using_index_permalinks() )
  362. $redirect['path'] = str_replace( '/' . $wp_rewrite->index . '/', '/', $redirect['path'] );
  363. // trailing slashes
  364. if ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() && !is_404() && (!is_front_page() || ( is_front_page() && (get_query_var('paged') > 1) ) ) ) {
  365. $user_ts_type = '';
  366. if ( get_query_var('paged') > 0 ) {
  367. $user_ts_type = 'paged';
  368. } else {
  369. foreach ( array('single', 'category', 'page', 'day', 'month', 'year', 'home') as $type ) {
  370. $func = 'is_' . $type;
  371. if ( call_user_func($func) ) {
  372. $user_ts_type = $type;
  373. break;
  374. }
  375. }
  376. }
  377. $redirect['path'] = user_trailingslashit($redirect['path'], $user_ts_type);
  378. } elseif ( is_front_page() ) {
  379. $redirect['path'] = trailingslashit($redirect['path']);
  380. }
  381. // Strip multiple slashes out of the URL
  382. if ( strpos($redirect['path'], '//') > -1 )
  383. $redirect['path'] = preg_replace('|/+|', '/', $redirect['path']);
  384. // Always trailing slash the Front Page URL
  385. if ( trailingslashit( $redirect['path'] ) == trailingslashit( $user_home['path'] ) )
  386. $redirect['path'] = trailingslashit($redirect['path']);
  387. // Ignore differences in host capitalization, as this can lead to infinite redirects
  388. // Only redirect no-www <=> yes-www
  389. if ( strtolower($original['host']) == strtolower($redirect['host']) ||
  390. ( strtolower($original['host']) != 'www.' . strtolower($redirect['host']) && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
  391. $redirect['host'] = $original['host'];
  392. $compare_original = array( $original['host'], $original['path'] );
  393. if ( !empty( $original['port'] ) )
  394. $compare_original[] = $original['port'];
  395. if ( !empty( $original['query'] ) )
  396. $compare_original[] = $original['query'];
  397. $compare_redirect = array( $redirect['host'], $redirect['path'] );
  398. if ( !empty( $redirect['port'] ) )
  399. $compare_redirect[] = $redirect['port'];
  400. if ( !empty( $redirect['query'] ) )
  401. $compare_redirect[] = $redirect['query'];
  402. if ( $compare_original !== $compare_redirect ) {
  403. $redirect_url = $redirect['scheme'] . '://' . $redirect['host'];
  404. if ( !empty($redirect['port']) )
  405. $redirect_url .= ':' . $redirect['port'];
  406. $redirect_url .= $redirect['path'];
  407. if ( !empty($redirect['query']) )
  408. $redirect_url .= '?' . $redirect['query'];
  409. }
  410. if ( ! $redirect_url || $redirect_url == $requested_url ) {
  411. return;
  412. }
  413. // Hex encoded octets are case-insensitive.
  414. if ( false !== strpos($requested_url, '%') ) {
  415. if ( !function_exists('lowercase_octets') ) {
  416. /**
  417. * Converts the first hex-encoded octet match to lowercase.
  418. *
  419. * @since 3.1.0
  420. * @ignore
  421. *
  422. * @param array $matches Hex-encoded octet matches for the requested URL.
  423. * @return string Lowercased version of the first match.
  424. */
  425. function lowercase_octets($matches) {
  426. return strtolower( $matches[0] );
  427. }
  428. }
  429. $requested_url = preg_replace_callback('|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url);
  430. }
  431. /**
  432. * Filters the canonical redirect URL.
  433. *
  434. * Returning false to this filter will cancel the redirect.
  435. *
  436. * @since 2.3.0
  437. *
  438. * @param string $redirect_url The redirect URL.
  439. * @param string $requested_url The requested URL.
  440. */
  441. $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );
  442. // yes, again -- in case the filter aborted the request
  443. if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) == strip_fragment_from_url( $requested_url ) ) {
  444. return;
  445. }
  446. if ( $do_redirect ) {
  447. // protect against chained redirects
  448. if ( !redirect_canonical($redirect_url, false) ) {
  449. wp_redirect($redirect_url, 301);
  450. exit();
  451. } else {
  452. // Debug
  453. // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
  454. return;
  455. }
  456. } else {
  457. return $redirect_url;
  458. }
  459. }
  460. /**
  461. * Removes arguments from a query string if they are not present in a URL
  462. * DO NOT use this in plugin code.
  463. *
  464. * @since 3.4.0
  465. * @access private
  466. *
  467. * @param string $query_string
  468. * @param array $args_to_check
  469. * @param string $url
  470. * @return string The altered query string
  471. */
  472. function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) {
  473. $parsed_url = @parse_url( $url );
  474. if ( ! empty( $parsed_url['query'] ) ) {
  475. parse_str( $parsed_url['query'], $parsed_query );
  476. foreach ( $args_to_check as $qv ) {
  477. if ( !isset( $parsed_query[$qv] ) )
  478. $query_string = remove_query_arg( $qv, $query_string );
  479. }
  480. } else {
  481. $query_string = remove_query_arg( $args_to_check, $query_string );
  482. }
  483. return $query_string;
  484. }
  485. /**
  486. * Strips the #fragment from a URL, if one is present.
  487. *
  488. * @since 4.4.0
  489. *
  490. * @param string $url The URL to strip.
  491. * @return string The altered URL.
  492. */
  493. function strip_fragment_from_url( $url ) {
  494. $parsed_url = @parse_url( $url );
  495. if ( ! empty( $parsed_url['host'] ) ) {
  496. // This mirrors code in redirect_canonical(). It does not handle every case.
  497. $url = $parsed_url['scheme'] . '://' . $parsed_url['host'];
  498. if ( ! empty( $parsed_url['port'] ) ) {
  499. $url .= ':' . $parsed_url['port'];
  500. }
  501. $url .= $parsed_url['path'];
  502. if ( ! empty( $parsed_url['query'] ) ) {
  503. $url .= '?' . $parsed_url['query'];
  504. }
  505. }
  506. return $url;
  507. }
  508. /**
  509. * Attempts to guess the correct URL based on query vars
  510. *
  511. * @since 2.3.0
  512. *
  513. * @global wpdb $wpdb WordPress database abstraction object.
  514. *
  515. * @return false|string The correct URL if one is found. False on failure.
  516. */
  517. function redirect_guess_404_permalink() {
  518. global $wpdb;
  519. if ( get_query_var('name') ) {
  520. $where = $wpdb->prepare("post_name LIKE %s", $wpdb->esc_like( get_query_var('name') ) . '%');
  521. // if any of post_type, year, monthnum, or day are set, use them to refine the query
  522. if ( get_query_var('post_type') )
  523. $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
  524. else
  525. $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
  526. if ( get_query_var('year') )
  527. $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
  528. if ( get_query_var('monthnum') )
  529. $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
  530. if ( get_query_var('day') )
  531. $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
  532. $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
  533. if ( ! $post_id )
  534. return false;
  535. if ( get_query_var( 'feed' ) )
  536. return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
  537. elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) )
  538. return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
  539. else
  540. return get_permalink( $post_id );
  541. }
  542. return false;
  543. }
  544. /**
  545. * Redirects a variety of shorthand URLs to the admin.
  546. *
  547. * If a user visits example.com/admin, they'll be redirected to /wp-admin.
  548. * Visiting /login redirects to /wp-login.php, and so on.
  549. *
  550. * @since 3.4.0
  551. *
  552. * @global WP_Rewrite $wp_rewrite
  553. */
  554. function wp_redirect_admin_locations() {
  555. global $wp_rewrite;
  556. if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) )
  557. return;
  558. $admins = array(
  559. home_url( 'wp-admin', 'relative' ),
  560. home_url( 'dashboard', 'relative' ),
  561. home_url( 'admin', 'relative' ),
  562. site_url( 'dashboard', 'relative' ),
  563. site_url( 'admin', 'relative' ),
  564. );
  565. if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins ) ) {
  566. wp_redirect( admin_url() );
  567. exit;
  568. }
  569. $logins = array(
  570. home_url( 'wp-login.php', 'relative' ),
  571. home_url( 'login', 'relative' ),
  572. site_url( 'login', 'relative' ),
  573. );
  574. if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins ) ) {
  575. wp_redirect( wp_login_url() );
  576. exit;
  577. }
  578. }