How To Format SEO URL With Slashes In Magento?

how to format seo url with slashes in magento

TASK: Adding slashes into SEO URL in Magento

From: https://www.example.com/products?p=1&attributes=value

To: https://www.example.com/products/p/1/attributes/value

SOLUTIONS

You can override the getUrl() method in the class Mage_Catalog_Model_Layer_Filter_Item, path /app/code/core/Mage/Catalog/Model/Layer/Filter/Item.php

Then, locate the function at line 57, it will 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));
  }

In file config.xml of your custom module, rewrite class Mage_Catalog_Model_Layer_Filter_Item:

<models>
            ....
            <catalog>
    <layer_filter_item>Namespace_Module_Model_Catalog_Layer_Filter_Item</layer_filter_item>
<catalog>
....
</models>

Overriding function getUrl() of class Mage_Catalog_Model_Layer_Filter_Item in class Namespace_Module_Model_Catalog_Layer_Filter_Item
Example with category:

public function getUrl()
{
    if($this->getFilter()->getRequestVar() == "cat"){ // or �color’, �price’,...
        $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));
    }
}

We have shown you some simple steps in order to add slashes into the SEO URL in Magento. If you have any problems when following this Magento tutorial, be free to leave a comment below. See you in the next tutorials!

See More Our Magento “How To” Series:

6 Easy Steps To Generate URL Rewrite in Magento 2

How To Use Flat Catalog In Magento 2?

How To Config URL Rewrite in Magento 2?

How To Set Up Automatic Product Redirects in Magento 2?

Leave a Reply

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