SEO Canonical URLs

Apr 20 2024
Codeed Inc
canonical-urls

Canonical URLs play a crucial role in SEO (Search Engine Optimization) by helping search engines understand the preferred version of a web page when multiple URLs point to the same content. This is particularly important for avoiding duplicate content issues, which can negatively impact a website’s search engine rankings.

Why are Canonical URLs Important?

Avoid Duplicate Content Issues: Search engines may see different URLs with the same content as duplicate content, which can dilute the SEO value of your pages. Canonical URLs help consolidate ranking signals for the preferred version of the content.

Consolidate Ranking Signals: When there are multiple versions of a page (e.g., HTTP and HTTPS, www and non-www), using canonical tags can consolidate the ranking signals to the preferred version, helping to improve the page’s SEO performance.

Improve Crawl Efficiency: By specifying the canonical URL, you help search engines understand which version of the page to crawl and index, reducing crawl inefficiencies.

How to Implement Canonical URLs?

You can implement canonical URLs using the <link rel=”canonical” href=”https://www.example.com/original-page/” /> tag in the HTML <head> section of your web pages. Replace https://www.example.com/original-page/ with the URL of the preferred version of your content.

For example:

<head>

    <link rel=”canonical” href=”https://www.example.com/original-page/” />

</head>

 

Best Practices for Canonical URLs:

  • Choose a Preferred Version: Decide on the preferred version of your URLs (e.g., www vs non-www, HTTP vs HTTPS) and stick to it consistently across your website.
  • Use Absolute URLs: Always use absolute URLs (include the full URL) in the canonical tag to avoid confusion.
  • Regularly Check Canonical Tags: Monitor your website regularly to ensure that canonical tags are correctly implemented and pointing to the right URLs.
  • Avoid Self-Referencing Canonicals: Ensure that you don’t accidentally set a canonical tag pointing to the same page it’s on. This can lead to confusion.
  • Use Tools: Utilize SEO tools like Screaming Frog, SEMrush, or Moz to check for canonicalization issues on your website and ensure proper implementation

Yoast SEO Canonical URLs – API documentation

To change the canonical URL that Yoast SEO generated for a URL programmatically, you can use the wpseo_canonical filter.

Change the canonical URL

For example, the following code would change the canonical on a page with ID 12345:

         /**

           * Filters the canonical URL.

          *

         * @param string $canonical The current page’s generated canonical URL.

        *

         * @return string The filtered canonical URL.

          */

             function prefix_filter_canonical_example( $canonical ) {

                    if ( is_page( 12345 ) ) {

                        $canonical = ‘https://example.com/canonical-url/’;

                      }

                  return $canonical;

           }

          add_filter( ‘wpseo_canonical’, ‘prefix_filter_canonical_example’ );

Remove the canonical URL

If you want to completely remove the canonical URL from a page, simply return false for that page, like so:

/**

* Filter the canonical URL.

*

* @param string $canonical The current page’s generated canonical URL.

*

* @return bool|string The filtered canonical URL or false when we want to remove it.

*/

function prefix_filter_canonical_example( $canonical ) {

 if ( is_page( 12345 ) ) {

   return false;

 }

 return $canonical;

}

add_filter( ‘wpseo_canonical’, ‘prefix_filter_canonical_example’ );

Or to remove it from all pages, simply do:

add_filter( ‘wpseo_canonical’, ‘__return_false’ );

 

https://developer.yoast.com/features/seo-tags/canonical-urls/api/

https://support.google.com/webmasters/answer/10347851?hl=en#:~:text=A%20canonical%20URL%20is%20the,Google%20chooses%20one%20as%20canonical.

 

 

×

Hello!

Click below to chat on WhatsApp.

× Need Help?