Yoast SEO: Canonical URLs on a Site with Optional SSL


When we moved our site over to Flywheel, we didn’t force everyone into an SSL connection (the https:// url that ensures a secure connection). We’re still undecided on this. We may ensure that form submissions and the ecommerce portion are secure, but not sure about just the average article to read.

With that in mind, we realized that our canonical links were showing up both secure and insecure. I’ve not read a lot on the topic, but that seems that it could be problematic if Google is treating each path separately. Indeed, within Webmasters, we had to register the secure site separately so I can only guess that it would cause confusion.

What is a Canonical Link?

A canonical link element is an element in the head section of an HTML page (invisible to users) that guides search engines to the preferred version of a web page. This is an essential element when optimizing your site for search engines since you want to ensure any authority passed through links goes to the appropriate URL. The vast majority of content management systems produce multiple paths to identical content. Without a canonical to define the appropriate path, your authority could be split between multiple paths to the same content.

In reviewing the Yoast SEO plugin knowledge base, the plugin simply pulls the permalink through WordPress’ standard function. In other words, if you’re on a secure page, it’s going to list the https path, if you’re not – it’s going to list the http path. Ugh.

Within our theme’s functions.php file, and using Yoast’s canonical filter wpseo_canonical, we added the following function to force all canonical links to the secure URL:

function mtb_canonical_ssl($url) {
   $url = preg_replace("/^http:/i", "https:", $url);
   return $url;
add_filter( 'wpseo_canonical', 'mtb_canonical_ssl' );

Now, regardless of which path a user goes to or how the Google crawler captures the canonical, it will only show up as the secure page with the https:// URL path. The Yoast plugin used to have the option to define this, but it appears it was depricated from the plugin.


  1. 1
  2. 2

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.