Fix Category Layered Navigation and add “rel=nofollow” to all others

I was struggling with a way to change the layered navigation so that categories and sub-categories would display the actual url but have any other parameters use ‘rel=nofollow’ so that the duplicate page content would be minimized.

The solution is a 2-step process that changes the “cat=xx” parameter to the proper url and then another step checks the name of the parameter and adds “rel=nofollow” if it’s not a category navigation link.

STEP 1 – Change the category/subcategory to the actual link.

(The complete instructions for this step are located at the BlueClaw Posting

COPY: app/code/core/Mage/Catalog/Model/Layer/Filter/Item.php
TO: app/code/local/Mage/Catalog/Model/Layer/Filter/Item.php

Make the folowing change in the getUrl fuction as shown below:


public function getUrl()
{

// changed section below to convert filter to category url instead of cat=
//$query = array(
// $this->getFilter()->getRequestVar()=>$this->getValue(),
// Mage::getBlockSingleton('page/html_pager')->getPageVarName() => null // exclude current page from urls
//);
//return Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));

if($this->getFilter()->getRequestVar() == "cat"){
$category_url = Mage::getModel('catalog/category')->load($this->getValue())->getUrl();
$return = $category_url;
// This section has been commented out to ignore all url parameters (such as 'p=', 'order=' - it can be uncommented and use the preg_replace to ignore a specific parameter
//$request = Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
//if(strpos($request,'?') !== false ){
// $query_string = substr($request,strpos($request,'?'));
//}
//else{
// $query_string = '';
//}
//if(!empty($query_string)){
//$query_string = preg_replace('/(?:&|(\?))' . 'p' . '=[^&]*(?(1)&|)?/i', "$1", $query_string);
// $return .= $query_string;
//}
return $return;
}
else{
$query = array(
$this->getFilter()->getRequestVar()=>$this->getValue(),
Mage::getBlockSingleton('page/html_pager')->getPageVarName() => null // exclude current page from urls
);

return Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));
}
}

Install the change and make sure everything is working properly.

Once that’s completed, there’s one more change that will add “rel=nofollow” to any other parameter that’s NOT category.

OPEN: app/design/frontend/default/YOURTHEME/template/catalog/layer/filter.phtml

Change the line in the foreach loop as shown below:


<?php foreach ($this->getItems() as $_item): ?>
<li>
<?php if ($_item->getCount() > 0): ?>
<?php // added if section below to display nofollow if navigation name is NOT category ?>
<a <?php if(!preg_match("/category/i", $this->getName())) { echo "rel=\"nofollow\""; } ?> href="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel() ?></a>
<?php else: echo $_item->getLabel() ?>
<?php endif; ?>
<?php if ($this->shouldDisplayProductCount()): ?>
(<?php echo $_item->getCount() ?>)
<?php endif; ?>
</li>
<?php endforeach ?>

Install the change and check the source for the page – you should notice that the category filters have the correct url (with “nofollow” as before) and any other filter will have “rel=”nofollow” as part of the link.

Leave a Reply