Prevent automatic URL rewrites created that include category URL key in product URL

Prevent automatic URL rewrites created that include category URL key in product URL

August 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Prevent automatic URL rewrites created that include category URL key in product URL

Preventing Unnecessary Product URL Rewrites in Magento

If you’ve ever found yourself overwhelmed by the sheer number of URL rewrites that Magento creates for your products, especially when using categories, rest assured—you’re not alone. By default, Magento is quite generous with its URL rewrite generation, which can lead to a proliferation of duplicate or near-duplicate content if not managed properly.

Understanding the Problem

Let’s break down the issue step-by-step:

  1. You have a product named “Product Name” assigned to multiple categories.

  2. The categories are structured something like this: Root > Subcategory > Third Level.

  3. When you save or update your product, Magento automatically creates URL rewrites for each combination of category and subcategory. This means it will generate rewrites with the following paths:

    • /root/product-name
    • /root/subcategory/product-name
    • /root/subcategory/third-level/product-name
  4. Even though you’ve set “Use Categories Path for Product URLs” to “No” in System > Configuration > Catalog > Search Engine Optimizations, Magento still creates these additional rewrites.

  5. The concern is that search engines could potentially index these URLs as separate pages, leading to duplicate content issues and potential SEO penalties.

Why Magento Behaves This Way

At its core, this behavior is a result of how Magento handles URL routing. It maintains a wide net of routing rules in an effort to provide maximum flexibility for your store’s structure. However, this can sometimes lead to unnecessary complexity when dealing with URL rewrites.

Solving the Problem: Custom Rewrite Generation

The most effective way to resolve this issue is to manually control the process of creating product URL rewrites. You can achieve this by overriding Magento’s default rewrite generation logic in your custom themes or extensions.

Here’s a high-level overview of the process:

  1. Create an Observer: Implement a module that observes the catalog_product_save_after event.
  2. Clear Existing Rewrites: In your observer, clear all existing URL rewrites for the product using $product->getUrlRewriteCollection()->delete();.
  3. Recreate Necessary Rewrites: Recreate only the necessary rewrite—ideally, just the one without any category paths.

Here’s a simple example of how you might structure this in your module:

class RewriteGeneratorObserver implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $product = $observer->getProduct();
        $categoryIds = $product->getCategoryIds();

        // Clear existing rewrites
        $rewriteCollection = $product->getUrlRewriteCollection()->addFilter('product_id', $product->getId())->load();
        foreach ($rewriteCollection as $rewrite) {
            $rewrite->delete();
        }

        // Recreate necessary rewrite (you can customize this logic)
        $urlRewriteModel = Mage::getSingleton('core/resource')->getConnection('core_write');
        $query = "INSERT INTO core_url_rewrite (store_id, id_path, request_path, target_path) VALUES ('0', '{$product->getId()}', '{$product->getUrlKey()}', '/{$product->getUrlKey()}')";
        $urlRewriteModel->query($query);
    }
}

Remember to register your observer in the config.xml file of your module:

<global>
  <events>
    <catalog_product_save_after>
      <observers>
        <rewrite_generator_observer>
          <type>singleton</type>
          <class>Namespace_Module_Model_RewriteGeneratorObserver</class>
          <method>execute</method>
        </rewrite_generator_observer>
      </observers>
    </catalog_product_save_after>
  </events>
</global>

Conclusion

While Magento’s default behavior of creating extensive URL rewrites can be frustrating, the solution lies in taking control of this process through custom module development. By overriding the standard product save event and manually managing your site’s rewrite generation, you can ensure that only necessary rewrites are created.

This approach provides a clean, controlled environment for your site’s URLs, reducing the risk of duplicate content issues and improving overall SEO performance. It also aligns with Magento 2 best practices, which encourage customization and extension development to meet specific business needs.

For more in-depth guidance on Magento module development and event observation, I highly recommend checking out our comprehensive guide at https://laravelcompany.com/magento-development/observers. This resource provides a detailed walkthrough of the process, along with practical examples to help you get started.

Remember, every Magento store is unique, and your specific configuration may require additional tweaks or considerations. Always test changes in a staging environment before deploying them to your live site to ensure they don’t introduce any unexpected issues.

With this approach, you can confidently manage your product URLs and maintain a clean, optimized SEO strategy for your Magento store.

Catalog Indexing Performance API

Our network