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 Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

1 day ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago