Magento 2 | Add NOINDEX, NOFOLLOW to CMS page using Layout Update XML

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>

It looks like this is a bug in Magento. You should be able to update HEAD tags or specifically the robots META tags using XML in the Layout Update XML field.

Here is a link to the issue on Github with the solution. https://github.com/magento/magento2/issues/4454

After vendor/magento/framework/View/Layout/etc/page_layout.xsd has been fixed you should be able to use

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

or

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

in the Layout Update XML field of the page in order to update the robots META tag.

Tags:

Xml

Magento2