Magento 2 XML – Add NOINDEX, NOFOLLOW to CMS Page Using Layout Update XML

magento2xml

How do I add the following robots META tag to a CMS page in Magento 2 using the Layout Update XML field?

<meta name="robots" content="noindex,nofollow"/>

I have tried the suggestions in the following questions, but none of them work

For example when I add the following code to the Layout Update XML field:

<head>
    <meta name="robots" content="NOINDEX,NOFOLLOW"/>
</head>

I get this error

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

Best Answer

To set the robots to NOINDEX,NOFOLLOW for a specific CMS page with a URL key of no-route-2, I usually create 2 files. Yes, this can be done in one file but I usually find that there is more than one page that I want to set the robots for and I don't like repetitive code. I'll show you both ways.

Create file Magento_Theme/layout/noindex_nofollow.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">
    <head>
        <meta name="robots" content="NOINDEX,NOFOLLOW"/>
    </head>
</page>

Create file Magento_Theme/layout/cms_page_view_id_no-route-2.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">
    <update handle="noindex_nofollow" />
</page>

After this, flush the cache and refresh the page and your robots for that one specific page will be updated. For any additional pages, just create duplicate the cms_page_view_id_no-route-2.xml file contents but be sure to name the new file appropriate to match the next page you are doing this for.

I should also mention, that you probably want to set this page's robots to NOINDEX,FOLLOW. That is because you dont want to index the page but you should want the search engines to continue crawling your site, assuming that the page contains mostly internal links.

In that case, you would do:

Create file Magento_Theme/layout/noindex_follow.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">
    <head>
        <meta name="robots" content="NOINDEX,FOLLOW"/>
    </head>
</page>

Create file Magento_Theme/layout/cms_page_view_id_no-route-2.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">
    <update handle="noindex_follow" />
</page>

The way to do this in one file for one specific page is:

Create file Magento_Theme/layout/cms_page_view_id_no-route-2.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">
    <head>
        <meta name="robots" content="NOINDEX,NOFOLLOW"/>
    </head>
</page>