Bobcares

Regenerate URL Rewrites Magento – The Right Way in Magento 2

PDF Header PDF Footer

Manually regenerate URL rewrites in Magento 2 with CLI or code using the elgentos module. Our Magento Support Team is always here to help you.

Regenerate URL Rewrites Magento – The Right Way in Magento 2

regenerate url rewrites magentoIf you’re searching for how to regenerate URL rewrites Magento, here’s the reality: Magento 2 no longer does this automatically like Magento 1.x did. Thischange was made for performance reasons. The automatic reindexer in Magento 1.x slowed down stores with large catalogs, especially during imports. Magento 2 fixed the performance issue, but took away automatic URL regeneration by relying more on scheduled tasks like cron jobs.

Why URL Rewrites Matter

Instead of using messy links like:

http://yourstore.com/catalog/product/view/id/14451/s/12576660/category/732/

Magento URL rewrites turn that into SEO-friendly links like:

http://yourstore.com/spices/salt

These user-friendly URLs improve both search engine visibility and user experience. However, in Magento 2, this doesn’t happen automatically, especially during product imports. So, if you’re importing products or categories, you’ll need to regenerate URL rewrites Magento manually.

The Best Solution: elgentos’ regenerate-catalog-urls Module

The Magento2 Blog initially recommended a module by Lazel, but it’s outdated. Instead, use its fork: regenerate-catalog-urls by elgentos.

This module adds CLI commands to Magento 2 that regenerate:

  • Product rewrite URLs based on their URL path
  • Category rewrite URLs based on their URL path
  • Category URL paths based on their URL key and parent categories

It’s lightweight, efficient, and gets the job done.

Using It in Code

If you want tighter integration, especially for bulk imports, run it through code. Here’s the code block you’ll need to use:

foreach($list as $product) {
if($store_id === Store::DEFAULT_STORE_ID)
$product->setStoreId($store_id);
$this->urlPersist->deleteByData([
UrlRewrite::ENTITY_ID => $product->getId(),
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::REDIRECT_TYPE => 0,
UrlRewrite::STORE_ID => $store_id
]);
try {
$this->urlPersist->replace(
$this->productUrlRewriteGenerator->generate($product)
);
} catch(\Exception $e) {
$out->writeln('Duplicated url for '. $product->getId() .'');
}
}

This snippet taps into \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator (injected as $productUrlRewriteGenerator) and uses \Magento\UrlRewrite\Model\UrlPersistInterface (injected as $urlPersist) to manually replace old rewrites, which can sometimes lead to dependency injection issues like those explained here.

You can either loop through all products or target specific ones. Also, every URL rewrite is stored in the url_rewrite table, just like Magento 1.

[If needed, Our team is available 24/7 for additional assistance.]

Conclusion

To summarize, regenerate URL rewrites Magento is no longer a built-in feature in Magento 2. But thanks to community modules like elgentos’ regenerate-catalog-urls, and the flexibility of Magento’s architecture, you can still automate it, either via command line or code.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Get featured on the Bobcares blog and share your expertise with a global tech audience.

WRITE FOR US
server management

Spend time on your business, not on your servers.

TALK TO US

Or click here to learn more.

Speed issues driving customers away?
We’ve got your back!