2

I want to set

<action method="setRobots">
    <value>NOINDEX,FOLLOW</value>
</action>

For category page but only when any filter is selected.

So I did the following..

<catalog_category_layered translate="label">
    <reference name="head">
        <action method="setRobots">
            <value>NOINDEX,FOLLOW</value>
        </action>
    </reference>
</catalog_category_layered>

But it's loading that tag when any filter is not selected.

So basically

When URL is like as following

http://www.example.com/category/cate1.html

I want to it to be indexed. But when URL is like following

http://www.example.com/category/cate1.html?size=xl

I don't want it be indexed.

Any suggestion what should I do??

3 Answers 3

3

As an alternative you can use an helper method to check if filters are being applied.

<catalog_category_layered translate="label">
    <reference name="head">
        <action method="setRobots">
            <value helper="module/getRobots" />
        </action>
    </reference>
</catalog_category_layered>

For that you need to create a custom module.

Vendor/Module/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Vendor_Module>
            <version>1.0.0</version>
        </Vendor_Module>
    </modules>
    <global>
        <helpers>
            <module>
                <class>Vendor_Module_Helper</class>
            </module>
        </helpers>
    </global>
</config>

Then in your module helper file Vendor/Module/Helper/Data:

<?php

class Vendor_Module_Helper_Data extends Mage_Core_Helper_Abstract {
    public function getRobots()
    {
        $filters= Mage::getSingleton('catalog/layer')->getState()->getFilters();
        if (!is_array($filters)) {
            return 'NOINDEX,FOLLOW';
        } else {
            return 'INDEX,FOLLOW';
        }
    }
}

It's possible that this code does not work because the catalog layer singleton has not been instantiated yet.

If it's the case, I suggest you use the following code to check whether or not a GET parameter is being applied to the URL:

<?php

class Vendor_Module_Helper_Data extends Mage_Core_Helper_Abstract {
    public function getRobots()
    {
        $requestUri = Mage::app()->getRequest()->getRequestUri();
        if (strpos($requestUri, "?") !== false) {
            return 'NOINDEX,FOLLOW';
        } else {
            return 'INDEX,FOLLOW';
        }
    }
}
11
  • Okay, Let me try and will let you know soon. Thanks. Commented Jun 7, 2016 at 11:23
  • Can you please tell me in which files I should make changes for getRobots() function?? I'm newbie for Magento.. Commented Jun 7, 2016 at 13:06
  • @KaushalSuthar you shouldn't have to but let me know if you get unwanted results Commented Jun 7, 2016 at 13:07
  • I'm learning Magento, For getRobots() function I've made changes in app\code\local\Mage\Page\Block\Html\Head.php but I guess $filters is empty all the time, and then in local.xml Commented Jun 7, 2016 at 13:11
  • @KaushalSuthar I think you misunderstood my answer, do not edit app\code\local\Mage\Page\Block\Html\Head.php you need to create your own custom module, see my edit Commented Jun 7, 2016 at 13:22
3

you can create and use robots.txt file. and you can add the urls which are not to be indexed.

create robots.txt file with following content

User-agent: *
# Paths (No clean URLs)
Disallow: /*?size=*
Disallow: /*?brands=*
Disallow: /*?price=*
Disallow: /*?dir=*
Disallow: /*?cat=*

and upload this file to the root dir of magento

2
  • Good suggestion but what if I've too many different filters for different products so in this case I've to add too many line in .htaccess and I don't think it's proper solution for it. Commented Jun 7, 2016 at 11:14
  • you know the list of all product attributes which are of type layered navigation and you need to add this in the robots.txt not in .htaccess file Commented Jun 7, 2016 at 11:27
3

You can create a custom extension for this.
config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_MetaChange>
            <version>1.0.0</version>
        </Custom_MetaChange>
    </modules>
    <global>
        <models>
            <metaChange>
                <class>Custom_MetaChange_Model</class>
            </metaChange>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_layout_generate_xml_before>
                <observers>
                    <noindex>
                        <type>singleton</type>
                        <class>metaChange/observer</class>
                        <method>changeRobots</method>
                    </noindex>
                </observers>
            </controller_action_layout_generate_xml_before>
        </events>
    </frontend>
</config>

Observer.php

<?php

class Custom_MetaChange_Model_Observer extends Mage_Core_Model_Abstract {

    public function changeRobots($observer) {

        $myaction = $observer->getEvent()->getAction();
        $uri = $myaction->getRequest()->getRequestUri();
        if (stristr($uri, "?")) {
            $actionname = $myaction->getFullActionName();
            if (($actionname == 'catalog_category_layered') || ($actionname == 'catalog_category_layered_nochildren')) {
                $layout = $observer->getEvent()->getLayout();
                $product_info = $layout->getBlock('head');
                $layout->getUpdate()->addUpdate('<reference name="head"><action method="setRobots"><value>NOINDEX, FOLLOW</value></action></reference>');
                $layout->generateXml();
            }
        }

        return $this;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.