Category page NOINDEX,NOFOLLOW when filters applied
Category page NOINDEX,NOFOLLOW when filters applied.
Most websites are getting duplicated content issues at SEO when filters applied To avoid the issue we need to mark the category page’s meta robots value to `NOINDEX,NOFOLLOW` when the filter is applied on the category page.
Defined an observer on event layout_generate_blocks_after
<?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_generate_blocks_after">
<observer name="layout_generate_blocks_after"
instance="StackExchange\Magento\Observer\LayoutGenerateBlockAfter"/>
</event>
</config>
The Observer class code will be
<?php
namespace StackExchange\Magento\Observer;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Event\Observer;
use Magento\Framework\View\Page\Config;
class LayoutGenerateBlockAfter implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var Config
*/
private $pageConfig;
/**
* @var \Magento\Catalog\Model\Layer
*/
private $layerResolver;
/**
* @param Config $pageConfig
* @param Resolver $layerResolver
*/
public function __construct(
Config $pageConfig,
Resolver $layerResolver
) {
$this->pageConfig = $pageConfig;
$this->layerResolver = $layerResolver->get();
}
/**
* @inheritDoc
*/
public function execute(Observer $observer)
{
$fullActionName = $observer->getFullActionName();
if ($fullActionName !== "catalog_category_view"){
return;
}
$filters = $this->layerResolver->getState()->getFilters();
if (!is_array($filters)){
$filters = [];
}
if (!empty($filters)){
$this->pageConfig->setRobots('NOINDEX,NOFOLLOW');
}
}
}
Module is available at https://github.com/devamitbera/Magento2-extension/tree/master/Category-disallow/