Unable to set canonical for Magento 2 homepage
Add a canonical link to homepage with existing theme Ultimo:
- 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:
- app/code/[VendorName]/[ModuleName]/registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'[VendorName]_[ModuleName]',
__DIR__
);
- 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>
- 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>
<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.