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.
<link rel="canonical" href="https://www.cloudconfusing.com/article/" />
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:
https://www.cloudconfusing.com/article/
, but also https://www.cloudconfusing.com/page/?page=2
, and https://www.cloudconfusing.com/page/?page=3
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 (https://nghbrs.com/place/) 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 (https://nghbrs.com/place/) 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.
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 {
rel_canonical();
}
}
This is pretty simple code to implement. Basically, you are saying:
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.
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:
… 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.
Sal September 21st, 2022
Posted In: Web Development, Wordpress