5

I have tried setting below code but i get the below error.

<head>
    <link rel="canonical" src="https://www.example.com/" src_type="url"/>
</head> 

Please correct the XML data and try again. Element 'head': This element is not expected. Expected is one of ( referenceContainer, container, update, move ). Line: 1

3 Answers 3

6

Set homepage canonical url with theme:

  1. app/design/frontend/[VendorName]/[themename]/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

Set homepage canonical url with 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>
3

You can add the additions header as below inside body tag to the respective layout XML file, In your case cms_index_index.xml is the file.

<referenceContainer name="head.additional">
        <block class="Magento\Framework\View\Element\Template" name="head.rel.canonical" template="Vensdor_Module::template.phtml" />
    </referenceContainer>

In the template.phtml you can add canonical.

<link rel="canonical" href="https://www.example.com/" />
0

Add the following in the admin part

Goto Content > Design > Select the homepage ( per shop if you've more than one ) and Open up Design then in the block Layout Update xml write

> <head>
>       <link rel="canonical" src="<website home page adress>" src_type="url"/>   
> </head>

Reindex && clear cache

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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