redirect_canonical() – Redirects incoming links to the proper URL based on the site url.

You appear to be a bot. Output may be restricted

Description

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

Search engines consider www.somedomain.com and somedomain.com to be two different URLs when they both go to the same location. This SEO enhancement prevents 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, favicon.ico, 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.

Usage

$string|void = redirect_canonical( $requested_url, $do_redirect );

Parameters

$requested_url
( string ) optional – Optional. The URL that was requested, used to figure if redirect is needed.
$do_redirect
( bool ) optional default: 1 – Optional. Redirect to the new URL.

Returns

string|void The string of the URL, if redirect needed.

Source

File name: wordpress/wp-includes/canonical.php


Lines:

1 to 100 of 781
function redirect_canonical( $requested_url = null, $do_redirect = true ) {
  global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp;

  if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ), true ) ) {
    return;
  }

  // If we're not in wp-admin and the post has been published and preview nonce
  // is non-existent or invalid then no need for preview in query.
  if ( is_preview() && get_query_var( 'p' ) && 'publish' === get_post_status( get_query_var( 'p' ) ) ) {
    if ( ! isset( $_GET['preview_id'] )
      || ! isset( $_GET['preview_nonce'] )
      || ! wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . (int) $_GET['preview_id'] )
    ) {
      $wp_query->is_preview = false;
    }
  }

  if ( is_admin() || is_search() || is_preview() || is_trackback() || is_favicon()
    || ( $is_IIS && ! iis7_supports_permalinks() )
  ) {
    return;
  }

  if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
    // Build the URL in the address bar.
    $requested_url  = is_ssl() ? 'https://' : 'http://';
    $requested_url .= $_SERVER['HTTP_HOST'];
    $requested_url .= $_SERVER['REQUEST_URI'];
  }

  $original = parse_url( $requested_url );
  if ( false === $original ) {
    return;
  }

  $redirect     = $original;
  $redirect_url = false;
  $redirect_obj = false;

  // Notice fixing.
  if ( ! isset( $redirect['path'] ) ) {
    $redirect['path'] = '';
  }
  if ( ! isset( $redirect['query'] ) ) {
    $redirect['query'] = '';
  }

  /*
	 * If the original URL ended with non-breaking spaces, they were almost
	 * certainly inserted by accident. Let's remove them, so the reader doesn't
	 * see a 404 error with no obvious cause.
	 */
  $redirect['path'] = preg_replace( '|(%C2%A0)+$|i', '', $redirect['path'] );

  // It's not a preview, so remove it from URL.
  if ( get_query_var( 'preview' ) ) {
    $redirect['query'] = remove_query_arg( 'preview', $redirect['query'] );
  }

  $post_id = get_query_var( 'p' );

  if ( is_feed() && $post_id ) {
    $redirect_url = get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
    $redirect_obj = get_post( $post_id );

    if ( $redirect_url ) {
      $redirect['query'] = _remove_qs_args_if_not_in_url(
        $redirect['query'],
        array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type', 'feed' ),
        $redirect_url
      );

      $redirect['path'] = parse_url( $redirect_url, PHP_URL_PATH );
    }
  }

  if ( is_singular() && $wp_query->post_count < 1 && $post_id ) {

    $vars = $wpdb->get_results( $wpdb->prepare( "SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $post_id ) );

    if ( ! empty( $vars[0] ) ) {
      $vars = $vars[0];

      if ( 'revision' === $vars->post_type && $vars->post_parent > 0 ) {
        $post_id = $vars->post_parent;
      }

      $redirect_url = get_permalink( $post_id );
      $redirect_obj = get_post( $post_id );

      if ( $redirect_url ) {
        $redirect['query'] = _remove_qs_args_if_not_in_url(
          $redirect['query'],
          array( 'p', 'page_id', 'attachment_id', 'pagename', 'name', 'post_type' ),
          $redirect_url
        );

 View on GitHub View on Trac