Cloud Confusing

Explaining hosting, AWS, Wordpress, static sites, and all manner of cloud solutions.

Content management systems tend to ignore URL parameters, but sometimes you don’t want this to happen. Here is how you fix that with WordPress.

Custom Canonical Behavior

<link rel="canonical" href="" />

A canonical tag, like the one above, tells search engines that regardless of how many iterations of a page exists, the search engine should point at the main — the canonical — one. This is useful when you have something like:, but also, and because if you are coming to the article from a search engine, you probably want to start at the first page. The same goes for a list, directory, or anything else paginated. Or in the case of a search engine results page you don’t want Google to try to index every single possible output of that page, so you use a canonical.

But what if you are using the parameters in order to differentiate pages based on their content? Then you might want them to be differentiated by search engines. This might be the case if you have something where you use a parameter to filter the page’s data by geographic region:

Clearly those are cases where the country-specific information is worthwhile from an SEO standpoint and the root page ( isn’t interesting or appropriate as the canonical. It’s the parameter that determines the data, and the data is interesting.

WordPress will, in the scenario above, default to the base page ( as it’s the far and away more popular situation, so you need to do some custom work to fix the canonical for pages with parameters.

A Custom Canonical Fix In WordPress

If you want a no-plugin solution to custom canonicals in WordPress, it’s pretty easy.

Add to the bottom of your theme’s functions.php:

remove_action('wp_head', 'rel_canonical');
add_action('wp_head', 'new_rel_canonical');

function new_rel_canonical() {
if( is_page( 'super' )) {
global $wp;
$link = home_url( add_query_arg( NULL, NULL ) );
echo "<link rel='canonical' href='$link' />\n";
//echo "0000";//TEST
} else {

This is pretty simple code to implement. Basically, you are saying:

  • Remove the original call that creates the canonical
  • Add an action for a new canonical defined as follows
  • If my page has a slug of “super” (this could be the title, the article ID, whatever identifier you want)
  • Get global $wp in case you want to use this to get the URL
  • Get the absolute current URI (as opposed to relative or without params)
  • Print the link rel (the canonical) in the page head
  • echo “0000” will put a few zeros at the top of the page. This is useful if you want to test out your conditional and make sure it’s properly targeting the page in question
  • If this all fails print the original canonical

And that’s really all there is to it! It’s a simple fix and one that can be scaled pretty easily with that conditional statement.

A Cleaner Fix

At this point you’ve probably thought to yourself that avoiding URL parameters would have been the simple fix. You’d be right, but things aren’t always that simple. A URL setup like:

  • Seattle:
  • Jacksonville:
  • Nashville:

… would generally be preferable to the URL parameters. This method is cleaner, more search engine-friendly, easily to type if need be, and sends a strong signal that you have n differentiated pages, as opposed to a single page with a parameter (or parameters) used to filter the data that appears on it.

September 21st, 2022

Posted In: Web Development, Wordpress

© 2022 | Privacy Policy | About | UTM Creator | CVE Reporting