6

I'm trying to modify URL-Category Rules, from Category Hierarchy-Oriented rules to Seo-Oriented rules.

So i need to change URLs in this way.

From:

- Root-Category (URL: /rootcategory.html )

-- Sublevel category --> /rootcateogory/sublevelcategory.html

--- 3rd level Category --> /rootcategory/sublevelcategory/3rdlevelcategory.html

To:

- Root-Category (URL: /rootcategory.html )

-- Sublevel category --> /sublevelcategory.html

( without First level cateogory url key )

--- 3rd level Category --> /sublevelcategory/3rdlevelcategory.html

( without First level cateogory url key )

I'm in trouble whit Magento Admin configurations but it seems there is no way to do this.

Do i have to build a Magento Plugin ?

Any suggestions ?

EDIT

I'm running Magento EE ver. 1.13.1.0

2 Answers 2

3

Thats a pretty hard one.

You will either need to rewrite the whole url index generation, or you introduce an additional router which resolves your own url schema.

Read this link to get a basic idea of how to:
* http://inchoo.net/ecommerce/magento/custom-router-in-magento/

3

The code below is tested on CE-1.7.0.2. It should work for EE1.12. It looks like it doesn't work for EE-1.13.
The url of a category is determined by the url_path attribute of that category.
So you need to change the way that url_path is generated when adding a new category.
For that you need to rewrite Mage_Catalog_Model_Url::getCategoryRequestPath and change this line:

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

into this one:

if (in_array($category->getLevel(), array(2, 3))){
    $requestPath = $urlKey.$categoryUrlSuffix;
}
else {
    $requestPath = $parentPath . $urlKey . $categoryUrlSuffix;
}

This means that for level 2 and 3 categories (that are actually level 1 and 2 in the menu) you don't add in front of the url key the parent path.
The url path for the level 2 and 3 categories will consist only of the url_key you enter manually in the admin and the suffix (.html).

But there is a catch. This will only work for the new categories you add. The old ones will still have the classic path parent/child/grandchild.html.
TO change this behavior run the following code before adding new categories.

$collection = Mage::getResourceModel('catalog/category_collection')
     ->addAttributeToSelect('*')
     ->addAttributeToFilter('level', array('gt'=>2));

foreach ($collection as $category){
    $path = $category->getUrlPath();
    $parts = explode('/', $path);
    if (count($parts) > 1){
        unset($parts[0]);
        $path = implode('/', $parts);
    }
    $category->setUrlPath($path);
    Mage::getResourceSingleton('catalog/url')->saveCategoryAttribute($category, 'url_path');
}

The code above will change the url path for the existing categories in the desired format. Rebuild your indexes and everything will look as you want.

You will even get 301 redirects for the existing categories from parent/child/grandchild.html to child/grandchild.html

Note: backup your database just in case I'm wrong.

8
  • I'm trying to code your solution. I rewrote Mage_Catalog_Model_Url::getCategoryRequestPath but it is not called when New Sub Category is saved. In what database table should i check if the changes are done ?
    – alesdario
    Commented Feb 14, 2014 at 9:51
  • @alesdario. You should check the category menu on frontend. it's easier that way. Also make sure you reindexed the flat category after adding a category if the index is set on 'manual'. I tested the solution and it worked for me.
    – Marius
    Commented Feb 14, 2014 at 9:53
  • I don't know why Magento doesn't go through getCategoryRequestPath . I'm sure i'm wrong some where but i don't know where. Mage_Catalog_Model_Url is rewritten right, i've checked it with get_class(Mage::getModel('catalog/url'));
    – alesdario
    Commented Feb 18, 2014 at 12:05
  • @alesdario. This is strange indeed. I tested the code above and it worked.
    – Marius
    Commented Feb 18, 2014 at 12:11
  • i'll keep you aware. I'm using Magento EE , does it matter?
    – alesdario
    Commented Feb 18, 2014 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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