In my CE-1.9.0 store my products currently create the following url rewrites:


However in my store I want to be able to stop creating the rewrites for the category versions. I have System > Configuration > Catalog > Catalog > Use Categories Path for Product URLs set to No so these extra rewrites are unnecessary. Plus my store has around 500,000 products and counting so these extra url's are taking up a lot of space in the core_url_rewrite` table.

So my aim is to only be left with these versions:


If I truncate the core_url_rewrite table and reindex then only these versions get created so it is only when new products are created. My catalog is so large so I cannot keep truncated and rebuilding the table from scratch as the rebuilding takes several hours.

With help from this question I have found the file that is responsible for creating the rewrites when a product is created. It is:


However the answer is no longer valid as the code has changed in the core file.

In this file on line #538 there is the _afterLoad function it is the line


And this function _addUrlRewrite however i've tried changing a lot inside this function but have had no luck. The function in full is:

protected function _addUrlRewrite()
    $urlRewrites = null;
    if ($this->_cacheConf) {
        if (!($urlRewrites = Mage::app()->loadCache($this->_cacheConf['prefix'] . 'urlrewrite'))) {
            $urlRewrites = null;
        } else {
            $urlRewrites = unserialize($urlRewrites);

    if (!$urlRewrites) {
        $productIds = array();
        foreach($this->getItems() as $item) {
            $productIds[] = $item->getEntityId();
        if (!count($productIds)) {

        $select = $this->_factory->getProductUrlRewriteHelper()
            ->getTableSelect($productIds, $this->_urlRewriteCategory, Mage::app()->getStore()->getId());

        $urlRewrites = array();
        foreach ($this->getConnection()->fetchAll($select) as $row) {
            if (!isset($urlRewrites[$row['product_id']])) {
                $urlRewrites[$row['product_id']] = $row['request_path'];

        if ($this->_cacheConf) {
                $this->_cacheConf['prefix'] . 'urlrewrite',
                array_merge($this->_cacheConf['tags'], array(Mage_Catalog_Model_Product_Url::CACHE_TAG)),

    foreach($this->getItems() as $item) {
        if (empty($this->_urlRewriteCategory)) {
        if (isset($urlRewrites[$item->getEntityId()])) {
            $item->setData('request_path', $urlRewrites[$item->getEntityId()]);
        } else {
            $item->setData('request_path', false);

1 Answer 1


You have to rewrite Mage_Catalog_Model_Resource_Url.

In your config.xml:


Create a new Modle Resource/Url.php in your Module and override the method saveRewrite()


class Namespace_Module_Model_Resource_Url
    extends Mage_Catalog_Model_Resource_Url
    public function saveRewrite($rewriteData, $rewrite)
        if (!Mage::getStoreConfig(Mage_Catalog_Helper_Product::XML_PATH_PRODUCT_URL_USE_CATEGORY)
            && isset($rewriteData['category_id'])
            && $rewriteData['category_id'] > 0
        ) {
            return $this;

        return parent::saveRewrite($rewriteData, $rewrite);

You have to truncate the core_url_rewrite and start the catalog_url reindex. The code doesn't affect the Category URL Rewrite and works with product save and reindex.

  • Perfect, just what I needed
    – odd_duck
    Sep 12, 2016 at 14:01

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.