Restrict Magento incrementing numeric at the end of the URL after re-indexing

Restrict Magento incrementing numeric at the end of the URL after re-indexing

August 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Restrict Magento incrementing numeric at the end of the URL after re-indexing

Solving Magento URL Rewrite Incrementing Issue: A Comprehensive Guide

If you’re facing issues with Magento incrementing numeric values at the end of URLs after re-indexing, you’re not alone. This common problem can lead to an unnecessarily large core_url_rewrite table, causing performance degradation over time.

Understanding the Problem: Magento’s URL Rewrite Mechanism

When Magento encounters duplicate url_keys during re-indexing, it automatically creates new URL rewrite entries by appending incremented numeric suffixes. This is intended to ensure unique URLs for each product. However, in multi-site or multi-store environments with many products, this mechanism can quickly spiral out of control.

The core issue lies in Magento’s default behavior when generating unique URLs:

  1. It checks if the url_key already exists in the database.
  2. If it does, Magento increments a numeric suffix (_1, _2, etc.) and tries again until it finds a unique combination.
  3. These newly generated URLs are stored as ‘custom’ type URL rewrites.

Why This Becomes a Problem

The primary issue is that these incremented URLs aren’t deleted when the original product’s url_key changes. Over time, this leads to an accumulation of old, unused URL rewrite entries in your database. Moreover, Magento’s default indexers are designed to re-index all products during certain events (like saving a product), which can exacerbate the problem.

A Better Approach: Manual Control over URL Rewrites

Instead of relying on Magento’s automatic incrementing logic, you should take manual control over your URL rewrites. This involves two key strategies:

1. Implement Custom Logic for Unique URLs

You can create a custom script or module to handle the generation of unique url_keys and corresponding URLs. Here’s an example using PHP:

public function generateUniqueUrlKey($product)
{
    $baseKey = $this->generateBaseKey($product);
    $suffix = 1;

    while ($this->_urlRewriteRepository->getByData(
        [
            'request_path' => $baseKey . '_' . $suffix,
        ]
    )) {
        $suffix++;
    }

    return $baseKey . '_' . $suffix;
}

In this example, we’re generating a base key for the product and then checking if any existing URL rewrite matches that pattern with an incremented suffix. We continue incrementing until we find a unique combination.

2. Implement a Scheduled Cleanup Process

To prevent the accumulation of old, unused URL rewrites, you need to regularly clean up your database. You can create a scheduled cron job or use Magento’s built-in scheduled import/export feature for this purpose.

A typical cleanup script might look like this:

public function deleteUnusedUrlRewrites()
{
    $products = $this->productCollectionFactory->create();
    $productIds = $products->getAllIds();

    foreach ($this->_urlRewriteRepository->getList(new \Magento\Framework\Api\SearchCriteria()) as $rewrite) {
        if (!in_array($rewrite->getEntityTypeId(), $productIds)) {
            $this->_urlRewriteRepository->deleteById($rewrite->getId());
        }
    }
}

This script fetches all product IDs and then deletes any URL rewrites that don’t correspond to an existing product.

Best Practices for Managing Magento URLs

To avoid URL rewrite issues in the future:

  • Always ensure your url_keys are unique.
  • Avoid changing url_keys frequently, as each change triggers a re-indexing event.
  • Regularly clean up old or unused data from your database.

Remember, Magento’s default behavior serves a purpose, but it may not always be optimal for complex multi-site environments. Tailoring the URL rewrite process to your specific needs can lead to better performance and reduced data bloat in the long run.

Conclusion

Managing Magento URLs requires a delicate balance between ensuring uniqueness and avoiding unnecessary database clutter. By implementing custom logic for generating unique url_keys and regularly cleaning up old rewrites, you can effectively control this process and prevent it from spiraling out of control.

If you’re looking to optimize your e-commerce platform further, consider exploring our services at Laravel Company. Our team specializes in Magento development and can help you navigate complex issues like this one with ease.

Catalog Indexing Database Performance

Our network