How to Customize Magento Layered Navigation URLs to Improve SEO and User Experience

How to Customize Magento Layered Navigation URLs to Improve SEO and User Experience
by

Magento’s layered navigation system allows the customer to filter down the products presented to them from a category, enabling them to find products specific to their needs.

One flaw (in my opinion) is the the URL that is generated. Magento will append a query string containing filter parameters to the current category URL, resulting in unhelpful (not to mention non-SEO friendly) URLs such as www.yoursite.com/action-sports.html?manufacturer=120.

In most cases, and for attributes such as “price” or “manufacturer”, this isn’t really a concern or issue. However, when it comes to the category filter it can present a problem. To access, for example, the “Skateboard” category, you could visit either of:

www.yoursite.com/action-sports/skateboards.html

www.yoursite.com/action-sports.html?cat=12

The latter here is an obvious issue both for SEO (duplicate URL for the same content) and also for usability. By default, the title and description would still be that of the parent category (in this instance “A”) which could cause confusion to the user.

This can be fixed quite easily by overriding the getUrl() method in theMage_Catalog_Model_Layer_Filter_Item class (Accessible at:/app/code/core/Mage/Catalog/Model/Layer/Filter/Item.php)

I’d never recommend directly editing the core Magento code pool; so A) create a local copy of this file in the “local” code pool, or B) create your own Magento extension which rewrites the model.

The code

Locate the function at line 57, it should look like this:

public function getUrl()
{
    $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));
}

Now edit the code and put in a condition to check for the presence of the category filter (“cat” request variable). We first load the category object using its ID, and then retrieve the correct category URL.

The next section below (lines 7-12) will check if the current request includes any other query string parameters, and will build a new query string.

Lastly, this query string is added to the category URL and returned.

public function getUrl()
{
    if($this->getFilter()->getRequestVar() == "cat"){
        $category_url = Mage::getModel('catalog/category')->load($this->getValue())->getUrl();
        $return = $category_url;
        $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)){
            $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));
    }
}

This will help improve your SEO and user experience.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *