What are canonical URLs and how you can use them in Magento 2?

Canonical URLs in Magento 2

As a Magento 2 developer I enable canonical URLs on all my projects. In this article I will explain what canonical URLs are, why you should use them and how you can apply them to your Magento 2 website.

What are canonical URLs?

A canonical URL is a HTML element that tells search engines what the preferred version of the page they are viewing is. This ensures that only 1 URL is added to the search engines index. The main purpose of this element is to prevent duplicate content issues. Too many of these type of issues can lead to your SEO rankings falling. Duplicate content issues occur when the same page appears under multiple URLs. To avoid these SEO penalties a webmaster can apply a canonical URL <link> tag in the <head> of the page. See an example canonical url link tag for this page below:

<link rel=”canonical” href=”https://www.ryancopeland.co.uk/blog/canonical-urls-in-magento2″ />

This href attribute of this tag contains the URL that should be indexed by search engines.

Why use them in Magento 2?

In the Magento 2 configuration there is a setting called “Use Categories Path for Product URLs”. By default this setting is set to “No”, however some store owners enable this to aid the user experience.

With this setting enabled Magento 2 will add the category path to all products URLs. If a product exists in multiple categories, this will mean that multiple URLs will exist for the same product page. For example, using the sample data the “Atlas Fitness Tank” product would have the following URLs:

/atlas-fitness-tank.html
/men/atlas-fitness-tank.html
/men/tops-men/atlas-fitness-tank.html
/men/tops-men/tanks-men/atlas-fitness-tank.html

Enabling the canonical URL setting for both products and categories would ensure that there are no duplicate content penalty issues with your website. I will show you how to enable this setting below.

How can I enable canonical URLs in Magento 2?

To enable canonical URLs for both products and categories in Magento 2 simply follow the steps below:

Magento 2 Store Config
1. Login to the Magento 2 admin area and navigate to Stores -> Configuration.
Magento 2 Catalog -> SEO Config” class=”wp-image-2459″/><figcaption>2. Select the “Catalog” option from the right hand menu followed by the “Search Engine Optimisation” tab.</figcaption></figure>



<figure class=

3. Uncheck the “Use system value” checkbox and select “Yes” for both dropdown options highlighted. Once done click “Save Config”.

4. Finally, be sure to clear your config and full page caches after making this change to ensure your changes are applied to the frontend of your website.

Closing comments

Enabling canonical URLs will improve SEO on any Magento 2 website as it removes the risk of duplicate content penalties for all category and product pages. My advice to anyone would be to enable them regardless of whether or not you believe there is a risk or not. I hope you have found this article useful. Please get in touch if there is a particular Magento 2 subject you would like to know more about. I’m happy to create articles on demand.