7

I have added below code to my homepage by going to Content->Pages-> in layout xml under design. But doesn't seem to reflect in the frontend. I am using Magento 2.2.0 with Ultimo Theme

    <reference name="head">
  <action method="addLinkRel">
    <rel>canonical</rel>
    <href>https://www.example.com</href>
  </action>
</reference>

3 Answers 3

13
+350

Add a canonical link to homepage with existing theme Ultimo:

  1. app/design/frontend/Infortis/ultimo/Magento_Cms/layout/cms_index_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <link rel="canonical" src="https://www.example.com" src_type="url" />
    </head>
</page>

OR

You can try following code with new module:

  1. app/code/[VendorName]/[ModuleName]/registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[VendorName]_[ModuleName]',
    __DIR__
);
  1. app/code/[VendorName]/[ModuleName]/etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="[VendorName]_[ModuleName]" setup_version="1.0.0"/>
</config>
  1. app/code/[VendorName]/[ModuleName]/view/frontend/layout/cms_index_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <link rel="canonical" src="https://www.example.com" src_type="url" />
    </head>
</page>
4

<link> elements are not allowed in the homepage Layout Update XML.

You can add a canonical link to your homepage by creating or editing cms_index_index.xml in

app/design/frontend/{vendor}/{theme}/Magento_Cms/layout/

<?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">
    <head>
        <link rel="canonical" src="http://domain.com" src_type="url" />
    </head> 
</page>

Clear static files and cache and you're done.

2
  • How to add to search result page? Commented Dec 18, 2018 at 10:35
  • Add the same code to catalogsearch_result_index.xml in app/design/frontend/{vendor}/{theme}/Magento_CatalogSearch/layout/
    – Lez
    Commented Dec 18, 2018 at 20:19
-3

Try this below code. Please change the 'someurl' as per your requirements. And place it in layout section in admin.

  <link rel="canonical" src="someurl" src_type="url"/>
2
  • Doesn't seem to work.
    – Zoya
    Commented Oct 26, 2017 at 11:23
  • It isn't work for me. Commented Dec 29, 2017 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.