0

I am looking for help on a redirect rule for magento. I have recently removed a lot of discontinued products out of product categories for example:

http://www.example.com/gym-wear-brands/golds-gym-long-sleeve/gym/top.html

I have now removed the above product out of the gym-wear-brands category and the URL is now showing a 404 page.

The direct product URL below still works:

http://www.example.com/golds-gym-long-sleeve-gym-top.html

Is there a redirect rule in .htaccess file that would allow me to remove all sub categories from URLs so all product URLs including category path would then then redirect to the product URL? I have around 7000 in WMT that need to be addressed.

bumped to the homepage by Community 2 days ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • There is nothing in the path that indicates to .htaccess that it contains a category. You would have to add a rule for every single retired product URL that redirects appropriately. Your best bet is an extension in Magento, which has the necessary context. – jiheison 2 days ago
0

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 = '';
        //}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.