1

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

http://example.com/product-name
http://example.com/category/product-name
http://example.com/another-category/subcategory/product-name

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:

http://example.com/product-name

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:

app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php

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

$this->_addUrlRewrite($this->_urlRewriteCategory);

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)) {
            return;
        }

        $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) {
            Mage::app()->saveCache(
                serialize($urlRewrites),
                $this->_cacheConf['prefix'] . 'urlrewrite',
                array_merge($this->_cacheConf['tags'], array(Mage_Catalog_Model_Product_Url::CACHE_TAG)),
                $this->_cacheLifetime
            );
        }
    }

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

You have to rewrite Mage_Catalog_Model_Resource_Url.

In your config.xml:

<models>
    <catalog_resource>
        <rewrite>
            <url>Namespace_Module_Model_Resource_Url</url>
        </rewrite>
    </catalog_resource>
</models>

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

<?php

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 '16 at 14:01

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.