How To

How to set NOINDEX NOFOLLOW for Specific Page in Magento 2

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!

Click to rate this post!
[Total: 6 Average: 4.2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago