• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / 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:

701 to 732 of 732

  
/**
 * Filters the canonical redirect URL.
 *
 * Returning false to this filter will cancel the redirect.
 *
 * @since 2.3.0
 *
 * @param string $redirect_url  The redirect URL.
 * @param string $requested_url The requested URL.
 */
  $redirect_url = apply_filters( 'redirect_canonical', $redirect_url, $requested_url );

  // Yes, again -- in case the filter aborted the request.
  if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) === strip_fragment_from_url( $requested_url ) ) {
    return;
  }

  if ( $do_redirect ) {
    // Protect against chained redirects.
    if ( ! redirect_canonical( $redirect_url, false ) ) {
      wp_redirect( $redirect_url, 301 );
      exit;
    } else {
      // Debug.
      // die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
      return;
    }
  } else {
    return $redirect_url;
  }
}
 
« Previous [1] … [6] [7] [8]

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 1st September 2020

Primary Sidebar

Information

Function name: redirect_canonical
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/canonical.php
File ref: wp-includes/canonical.php
Deprecated?: No
API Letters: C,R

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites