2

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. :)

  • This seems a silly amount of effort. Email the admin if you dont have access and change the single setting designed for this. – ladle3000 Feb 11 '15 at 14:21
  • I appreciate your feedback. Can I just double check that setting definitely affects category pages and not only product pages? Obviously without access at present I cannot try it to find out! :) – Chris Philpot Feb 12 '15 at 7:39
  • Maybe you are right, I'm not finding that either for categores, though answer below claims differently. This option is available with seo extensions. – ladle3000 Feb 12 '15 at 12:12
3

It is hard to do it via htacces you only need to change few settings at the backend:

  1. Go to the Magento Admin Panel -> System -> Configuration -> Catalog -> Seo Options

  2. Select yes or no from "Use Parent Category Path for Category URLs"

  3. Refresh category url index

Option YES => http://www.example.com/category/subcategory.html

Option NO => http://www.example.com/subcategory.html

3

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

Try this in your .htaccess

#moving to new url structure
Redirect 301 http://domain.com/parent-category/subcategory.html http://domain.com/category.html

But It's Not a good practice every time you create a menu and then rewrite here. Then to much of 301 redirection is not good for SEO Also.

If you want you can try. But before you take Backup

0

Go to app/code/core/Mage/Catalog/Model/Url.php and copy this file to app/code/local/Mage/Catalog/Model/Url.php

Edit the copied file and around line number 726, you can find below code:

$requestPath = $parentPath . $urlKey . $categoryUrlSuffix;

change it to below code:

$requestPath = $urlKey . $categoryUrlSuffix;

Then update it.

Flush Magento cache and it should work.

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.