Magento – Remove parent categories from subcategory URLs

.htaccessurl-rewrite

I realise similar questions have been posed before, but I have an additional caveat when I ask the following question.

How can Magento URLs be rewritten such that:

http://www.example.com/category/subcategory.html

Becomes:

http://www.example.com/subcategory.html

without accessing the Admin menu or installing additional modules, and purely by editing the .htaccess file? Is there a way?

(If the .html can be stripped off the end as well, that would be terrific!)

Thank you for your time. 🙂

Best Answer

To remove category paths from product url set "Use Categories Path for Product URLs" to "No" under System -> Configuration -> Catalog -> Search Engine Optimizations.

To remove parent category path from child category url
Rewrite class Mage_Catalog_Model_Url
override method public function getCategoryRequestPath($category, $parentPath) like below

public function getCategoryRequestPath($category, $parentPath)
    {
        $storeId = $category->getStoreId();
        $idPath  = $this->generatePath('id', null, $category);
        $suffix  = $this->getCategoryUrlSuffix($storeId);

        if (isset($this->_rewrites[$idPath])) {
            $this->_rewrite = $this->_rewrites[$idPath];
            $existingRequestPath = $this->_rewrites[$idPath]->getRequestPath();
        }

        if ($category->getUrlKey() == '') {
            $urlKey = $this->getCategoryModel()->formatUrlKey($category->getName());
        }
        else {
            $urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey());
        }

        $categoryUrlSuffix = $this->getCategoryUrlSuffix($category->getStoreId());
        // Code commented to remove parent category path from child category path
        //if (null === $parentPath) {
            //$parentPath = $this->getResource()->getCategoryParentPath($category);
        //}
        //elseif ($parentPath == '/') {
            $parentPath = '';
        //}
        $parentPath = Mage::helper('catalog/category')->getCategoryUrlPath($parentPath,
                                                                           true, $category->getStoreId());

        $requestPath = $parentPath . $urlKey . $categoryUrlSuffix;
        if (isset($existingRequestPath) && $existingRequestPath == $requestPath . $suffix) {
            return $existingRequestPath;
        }

        if ($this->_deleteOldTargetPath($requestPath, $idPath, $storeId)) {
            return $requestPath;
        }

        return $this->getUnusedPath($category->getStoreId(), $requestPath,
                                    $this->generatePath('id', null, $category)
        );
    }


Modified below code

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

to

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