Magento 2 – Regenerate Url Rewrites
At our current Magento 2 project we discovered a major issue with url rewrites. All products are imported by an import script. Also all categories are created by this script. The problem: all product url rewrites are not created automatically. They are only created by saving each single category. I show you how to do it automatically…
Magento 2 – Regenerate Url Rewrites
Magento 1 had an indexer which regenerate url rewrites automatically each run of that indexer. This was quite comfortable. The only problem were shops with a lot of products. The more products and the more rewrites, the longer this indexer runs. So for performance optimization it was a good idea to remove this indexer for Magento 2. But this has other drawbacks…
Problem
An url rewrite is required for seo optimized shop urls. Normally a deeplink for a product looks like this:
1 | http://domain.com/catalog/product/view/id/11431/s/10546560/category/792/ |
As you can see, it is not a good idea, because numbers are not usable for a search machine. The best seo solution is to name an url by category name and product name. After you regenerate url rewrites the same link may look as follows:
1 | http://domain.com/fruits/banana |
Magento 2 do not automatically create rewrites. They are only computed if you do an action like save category. This makes sense if you create categories and products in you backend. If you import these things by code, you have to save all categories manually after each import. No solution for a programmer…
Solution
A quite simple solution is to use a ready to run Magento 2 module which does exactly the work of an rewrite indexer. A programmer did that an offers his module magento2-regenurl on github. You can call this regeneration by code or from command line. A better solution is to do it by code in your own importer module. The regeneration is quite simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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() .''); } } |
As you can see Magento 2 offers you a model for regenerate url rewrites \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator (productUrlRewriteGenerator) which you can inject into your model. This generated url rewrite can be replaced by \Magento\UrlRewrite\Model\UrlPersistInterface (urlPersist). You can run this in a loop for all products after import or for each single product after a change.
You find url rewrites in url_rewrite table in you database. This is exactly the same as in Magento 1.
Update
I recently found this Magento 2 Module on GitHub, which fixes all my problems. I can recomend it. You can simply regenerate all your url rewrites for products and categories with the following command:
1 | bin/magento ok:urlrewrites:regenerate |
Hope that helps!
Conclusion
Regenerate url rewrites is different in Magento 2. Now you have to think about it, because there is no indexer who does this automatically. You have to add some line of code to you importer model.
Thanks for the blog, I found the extension for Magento 2 which fulfil the exact requirement by command line.
http://shreejiinfosys.co.in/url-rewrite-generator-magento-2.html
thank you for your research and this link!
With this extension, you can regenerate the URL rewrites from the admin interface
https://marketplace.magento.com/webpanda-catalog-url-rewrites.html
Have had some major issues on this subject after trying to move data from a different system. Your post helped me get to my goal. Thank you.
Thank you for your comment! Nice to hear it helped.
Hello,
Thanks for sharing this useful and great tutorial.
I think you are missing only one step, which is to enable URL Rewrite from the admin panel of Magneto 2 store. During research, I have found this short guide to enable URL Rewrite from the admin panel: https://magenticians.com/magento-2-url-rewrite/
Thank you, but no. This setting is by default activated and not the source of described problem. It generates only rewrite for products that are generated under adminhtml and not for a custom import.
Ohh Got it! Thanks 🙂
Can you please let me know it is possible to generate only configurable products URLs?
This module is fantastic. It is a must when working with custom product/category imports.
Thanks, It is working. and proper solution for URL rewrites issue.
I really like your post. I really appreciate the good quality content you are posting here.
It’s a really interesting site.
cela ne fonctionne pas pour moi version 2.2.5
You can reset URL rewrites from admin area using this extension. It also allows you to customize the URLs and generate unique URLs by adding for ex the SKU in the URL
https://marketplace.magento.com/webpanda-catalog-url-rewrites.html