cancel
Showing results for 
Search instead for 
Did you mean: 

How do you add canonical tag to content pages on Magento 2?

SOLVED

How do you add canonical tag to content pages on Magento 2?

Hi All, 

 

I am a Magento Newbie and am having difficulty figuring out exactly where I would add canonical tag to content pages in Magento 2. We are having a duplicate content issue on content pages as multiple variants of urls are created based on the user path. I am trying to figure out exactly where I add the canonical html tag. I changed the setting for the products and categories. I would like to update the Home page as well as other content pages but I am not 100% certain where to do that. If someone can share the exact path on where I would update that I would really appreciate it. 

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How do you add canonical tag to content pages on Magento 2?

Hello @brandon_pagan ,

 

I hope that I gave you correct answer for content page(CMS) page to add canonical tag. Please follow below steps

 

  1. Go to Admin Panel > Content > Pages > Opne CMS Page > Design > Layout Update XML
  2. Add code in there
    <head>
          <link rel="canonical" src="www.example.com/page-url-key" src_type="url"/>
    </head>

--

If my answer is useful, please Accept as Solution & give Kudos

View solution in original post

5 REPLIES 5

Re: How do you add canonical tag to content pages on Magento 2?

Hello @brandon_pagan ,

 

I hope that I gave you correct answer for content page(CMS) page to add canonical tag. Please follow below steps

 

  1. Go to Admin Panel > Content > Pages > Opne CMS Page > Design > Layout Update XML
  2. Add code in there
    <head>
          <link rel="canonical" src="www.example.com/page-url-key" src_type="url"/>
    </head>

--

If my answer is useful, please Accept as Solution & give Kudos

Re: How do you add canonical tag to content pages on Magento 2?

Hey @gelanivishal and @brandon_pagan 

 

I think I know a better way to do it correct and conveniently.

 

1. Create your module.

2. Create your Helper, e.g. [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 . '" />';

    }
}

3. Create layout: [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>

4. Run bin/magento setup:upgrade

5. See if it works the way you wanted.

Now, on every CMS Page the canonical link will be added automatically.
Have a nice day.

Re: How do you add canonical tag to content pages on Magento 2?

It's not there. There's no "update XML" in that location.

Re: How do you add canonical tag to content pages on Magento 2?

Not there. can you please provide screenshot?

Re: How do you add canonical tag to content pages on Magento 2?

Here is a guide on how you can add canonical tags in Magento 2 for CMS, blog, contacts and other pages, including products and categories. 

 

Quick tip: it doesn't require code updating. So, it's a perfect option for non-techies like meSmiley Very Happy