Site icon MageComp Blog

How to set NOINDEX NOFOLLOW for Specific Page in Magento 2

How to Set NOINDEX NOFOLLOW for Specific Page in Magento 2(1)

Hello Magento Friends,

Today we will focus on How to set NOINDEX NOFOLLOW for Specific Page in Magento 2.

Magento allows managing instructions to index your website for web crawlers. You can control and prevent pages of your website to be indexed and crawled by search engines. For that, you can Configure Robots.txt in Magento 2.

NOINDEX stops web crawlers to crawl and index your webpage while NOFOLLOW stops web crawlers to follow links on the webpage. You can also Configure Robots.txt in Magento 2

Magento 2 gives the option to NOINDEX and NOFOLLOW for the whole site but whenever your site is live and sometimes you need to set NOINDEX and NOFOLLOW for a specific page then you need to follow the below steps

Steps to set NOINDEX NOFOLLOW for Specific Page in Magento 2:

Step 1:  First you need to create one event.xml file in the following path

app\code\Vendor\Extension\etc\frontend\event.xml

Now, add the below code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="add_robot_page" instance="Vendor\Extension\Observer\Noindexfollow" />
    </event>    
</config>

Step 2: Now you need to create Noindexfollow.php in the following path

app\code\Vendor\Extension\Observer\Noindexfollow.php

And, add the code as follows

<?php
namespace Vendor\Extension\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class Noindexfollow implements ObserverInterface
{
    protected $request;

    protected $layoutFactory;

    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\View\Page\Config $layoutFactory)
    {
            $this->request = $request;
            $this->layoutFactory = $layoutFactory;
    }
    public function execute(Observer $observer)
    {
        $fullActionName = $observer->getFullActionName();
        /* Here we give for category page, you can chages as per your need */
        if ($fullActionName == "catalog_category_view")
        {
                $this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
        }
    }
}

Conclusion:

By doing so you can set NOINDEX NOFOLLOW for Specific Page in Magento 2 easily. If you face any problem implementing the above steps, drop a comment below. Spread the article with your friends and stay connected with us!

Happy Coding!

Exit mobile version