The Most Popular Extension Builder for Magento 2

With a big catalog of 224+ extensions for your online store

How to Enable Canonical Meta Tag in Magento 2

The canonical meta tag in Magento 2 is a very important meta tag in SEO and using the canonical meta tag can improve your site ranking and concentrate pageviews. It helps a lot in solving duplicate content problems when there are multiple URLs containing identical or very similar content so that the search engines can index exactly your preferred URL. Scroll down to see the steps to enable Canonical Meta Tag for your Magento 2 store.

Steps to enable Canonical Meta Tag in Magento 2

Option 1

  1. On the Admin panel, click Stores. In the Settings section, select Configuration.
  2. Select Catalog under Catalog in the panel on the left
  3. Open the Search Engine Optimizations section, and continue with following:

How to Enable Canonical Meta Tag

Look at 2 last fields:

  • If you want search engines to index only pages that have a full category path, do the following:
    • In the Use Canonical Link Meta Tag for Categories field, select Yes
    • In the Use Canonical Link Meta Tag for Products field, select No
  • If you want search engines to index only product pages, do the following:
    • In the Use Canonical Link Meta Tag for Categories field, select No
    • In the Use Canonical Link Meta Tag for Products field, select Yes
  • If you want search engines to index both Categories and Products, do the following:
    • In the Use Canonical Link Meta Tag for Categories field, select Yes
    • In the Use Canonical Link Meta Tag for Products field, select Yes
  1. When completing, click Save Config.

Option 2

  1. Set up your module.

  2. Create a helper class, for example, [Namespace]/[Module]/Helper/Canonical.php.

<?php
declare(strict_types=1);

namespace [Namespace]\[Module]\Helper;

use Magento\Cms\Model\Page;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

/**
 * Class Canonical
 * @package [Namespace]\[Module]\Helper
 */
class Canonical extends AbstractHelper
{
    /**
     * @var Page
     */
    protected $cmsPage;

    /**
     * Canonical constructor.
     * @param Context $context
     * @param Page    $cmsPage
     */
    public function __construct(
        Context $context,
        Page $cmsPage
    ) {
        $this->cmsPage = $cmsPage;
        parent::__construct($context);
    }

    /**
     * This method is used in XML layout.
     * @return string
     */
    public function getCanonicalForAllCmsPages(): string
    {
        if ($this->cmsPage->getId()) {
            return $this->createLink(
                $this->scopeConfig->getValue('web/secure/base_url') . $this->cmsPage->getIdentifier()
            );
        }

        return '';
    }

    /**
     * @param $url
     * @return string
     */
    protected function createLink($url): string
    {
        return '<link rel="canonical" href="' . $url . '" />';
    }
} 
  1. Develop a layout file, for example, [[Namespace]/[Module]/view/frontend/layout/default.xml.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="head.additional">
            <container name="canonical.url.cms.page.container">
                <block class="Magento\Framework\View\Element\Text" name="canonical.url.cms.page">
                    <arguments>
                        <argument name="label" xsi:type="string">Adding canonical link with base URL</argument>
                        <argument name="text" xsi:type="helper" helper="[Namespace]\[Module]\Helper\Canonical::getCanonicalForAllCmsPages"/>
                    </arguments>
                </block>
            </container>
        </referenceBlock>
    </body>
</page> 
  1. Execute [bin/magento setup:upgrade to apply the changes and register the new module.

  2. Check if the result aligns with your desired outcome.

Recommendations

Increase traffic and boost sales with the most complete SEO solution for every store.

Check the Enabled Canonical URLs

The recommended best practice is to activate both of these settings. Once you have completed the configuration, click on Save, refresh your cache, and you should observe the canonical meta tag appearing on all your product pages and category pages.

Use CTRL + U to view your site source, and then CTRL + F to find canonical urls.

Check the Enabled Canonical URLs

Final words

Enabling Canonical Meta Tag in Magento 2 solves duplication issues and brings your blog SEO to the next level. You can also check our SEO extension out to have a more thorough solution, or read below related posts for more information in this topic. Feel free to address any concerns you are having with us in the comment box!

Related topics

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

Looking for Magento Development Company?

8+ years of experiences in e-commerce & Magento has prepared us for any challenges, so that we can lead you to your success.

Get free consultant
development service
x

    Explore Our Products:

    Subscribe

    Stay in the know

    Get special offers on the latest news from Mageplaza.

    Earn $10 in reward now!

    Earn $10 in reward now!

    comment
    iphone
    go up