Remove Parent Category Path From Sub Category Url In Magento



I have a website which has the 4 levels of the category and each and every category is coming in the url hence the url looks like as a very long string as http://www.abcxyz.com/test-category/test-category-level-1/test-category-level-1-1/test-category-level-1-2/test-category-level-1-3.html For this reason client wants to show only the current category name not the parent category name into the Url. Here in this post I am describing how to remove the Parent category path from sub category url in Magento. Here are the steps given below:

Step 1: Go to app/code/core/Mage/Catalog/Model/Url.php copy this file to app/code/local/Mage/Catalog/Model/Url.php because it is core Magento file so that we have to copy that file into the app/code/local.

Step 2: Now search for the getCategoryRequestPath($category, $parentPath) around line 698 in Magento ver. 1.9.1.0

Step 3: Search if (null === $parentPath) around line 717 and comment the line as below:

/* if (null === $parentPath) {
$parentPath = $this->getResource()->getCategoryParentPath($category);
}
elseif ($parentPath == '/'){*/
$parentPath = ''; //DO NOT Comment this line
//} 

Step 4: Now Go to the Magento admin and Clear Cache System->Cache Management and Reindex System->Index Management the data.

Step 5: Refresh your browser cache and navigate again now you will see only the Sub Category in Url not the Parent Category just like as http://www.abcxyz.com/test-category-level-1-3.html

Hope it helps, Thanks for reading.

Thanks