0

for sure this question has been asked so many times in different ways, but i didn't find anything usefull for my case.

We have an installation of Magento 1.9.3.6, the main task is to rewrite the pagination param to a SEO Friendly url param, for example:

http://www.example.com/category?p=2

to

http://www.example.com/category/p/2

we use nginx so the .htaccess ways is out, i'm trying to create a Router and an Observer (as the web suggest, maybe) that will rewrite the param if match, but i have one question:

If i allow magento to redirect the user to the correct page if in the URI there is "p=*" (with the Router & Observer way) it works, but how i REWRITE all the Links for example, in the pager? because also if magento allow to redirect to the correct page if there is a page param, the rendered urls in the whole rendered website are not rewrited.

Wich is the best way to do this? i need all the urls SEO Friendly and i won't use external modules.

Do you have any suggestions?

So many thanks in advance.

1 Answer 1

1

You could try to 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 the 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 p or 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));
    }
}

Let me know if it is useful

Kind regards

1
  • Unfortunately this trick work with the layered sidebar but not in the pager at the bottom of any products grid. I've tried to find a similar method for the pager and i've found that is not possible to change the url structure, moreover you can change the url structure but magento doesn't handle the link as you've edited it. There are 2 options, do the trick and add a sort of handler (router) for that kind of links or find some other trick maybe a best practice for doing this.
    – FreshHaze
    Commented Apr 6, 2018 at 7:57

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.