Today, we’re going to teach you guys how to set ‘no-follow and no-index tags for specific pages in a Magento 2 store.
Magento 2 allows you to control specific pages of your store to be crawled and indexed by search engines.
Noindex basically stops search engine crawlers from crawling & indexing the specified pages.
Nofollow, on the other hand, stops search engine crawlers from following links on specific pages.
Now, Magento 2 does come with an option to set Noindex & Nofollow tags for the entire store. But many times, you’ll have to set Noindex & Nofollow tags for only specific pages.
So, how does one can do that in Magento 2?
In this tutorial, we will show you exactly how to set Noindex & Nofollow tags for specific pages in your Magento 2 store.
How to Set Noindex & Nofollow for Specific Page in Magento 2?
Please follow the below steps to set Noindex & Nofollow tags for specific pages in your Magento 2 store.
Step 1. Create Event.xml File.
First of all, we need to create an event.xml file in the app\code\Vendor\Extension\etc\frontend\ folder and paste the following 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. Create Noindexfollow.php file.
After that, we also need to create a Noindexfollow.php file in the app\code\Vendor\Extension\Observer\ folder and paste the following code:
<?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
And there you go!
This is the easiest way to set Noindex & Nofollow tags for specific pages in your Magento 2 store.
And if you need our professional assistance, feel free to contact us at any time.