Remove Parent Category From Subcategory URL in Magento

In this post we will see how to remove parent category from subcategory url in Magento(CMS). When there exist 3rd or 4th child category in magento then subcategory url become lengthy and looks very long. For this reason we want to display only current category in URL instead of all parent category. So here in this tutorial we will see how to remove all parent category from url and keeps only current subcategory. Let us see the given below example:
Example:
Old URL = www.site.com/cat1/cat2/cat3/cat4.html
Changed to
New URL = www.site.com/cat4.html
Steps to follow for removing parent category from subcategory URL in Magento are as follows:

Step I)
Go to app/code/core/Mage/Catalog/Model/Url.php

copy the file to

app/code/local/Mage/Catalog/Model/Url.php

if not have folder just create it and copy file to it.

Step II)

Now open app/code/local/Mage/Catalog/Model/Url.php file in Editor

Step III)

Go to line no 717 inside the function getCategoryRequestPath($category, $parentPath){}

comment the line as shown below

 // if (null === $parentPath) {
     // $parentPath = $this->getResource()->getCategoryParentPath($category);
     // }
     // elseif ($parentPath == '/') {
            $parentPath = '';
     // }

Note: Do not comment $parentPath = ”;

Step IV)

After saved change.
Go to Admin ->System -> Cache Management -> Flush Cache Storage.
Go to Admin -> System -> Index Management and Reindex Data

Now refresh browser cache and open subcategory URL again and you will see only current subcategory name in URL not parent category name.
That’s it.

Leave a Reply

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