Question

I cannot get Magento 2 to renew URL rewrites for categories. I was forced to clear url_rewrites table after some weird problems happened after importing categories and products.

Now I get something like catalog/category/view/s/regntoj/id/304/.

I have tried the following

  • Save the category manually (doesn't work)
  • Change URL key and/or "content" and save the category manually (doesn't work)
  • "Marketing" > "URL rewrites" and add a rewrite rule (works)

I have more than 200 categories so going to Marketing > URL rewrites and manually setup each category will take a long time.

Was it helpful?

Solution

I suggest you do this in a setup script.

First, you will need to get a collection of \Magento\Catalog\Model\Category. You can do this by injecting the classes into the setup script constructor. I won't go too much into detail for this.

Then you would need to get an instance of Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator. There is no service contract that calls the model's method so you need to instantiate the class by injecting the Factory class in your constructor.

Then create a loop through your category collection and pass the object through CurrentUrlRewritesRegenerator->generate() function, like so:

// pseudo
    foreach ($categories as $category) {
        $urlRewritesRegenerator->generate($storeId, $category);
    }

Hope this helps.

OTHER TIPS

Run the module Iazel_RegenProductUrl to fix your URL rewrites, like that:

composer require iazel/module-regen-product-url 
php -f bin/magento setup:upgrade
php bin/magento iazel:regenurl

You can use this module which is a successor of the above mentioned, no deprecated, module by iazel:

https://github.com/peterjaap/magento2-regenurl

Follow the instructions in the repository.

It provides this commands:

Usage:
 regenerate:product:url [-s|--store="..."] [pids1] ... [pidsN]
 regenerate:category:url [-s]--store="..."] [cids1] ... [cidsN]
 regenerate:category:path [-s]--store="..."] [cids1] ... [cidsN]

This might happen because there is no URL rewrite in Magento. Check your URL rewrites, if that category doesn't exist then create it. In Marketing > URL re-writes > Add URL re-write:

Add URL re-write

Flush cache, reindex the site and try again.

Below are my steps which finally allow me to delete all the un-necessary URL-rewrites and make the category URL rewrite work in Magento 2.3.0

  1. Backup the database
  2. Run below in the database delete from catalog_url_rewrite_product_category; delete from url_rewrite where entity_type='category' or entity_type='product';
  3. System->Index Management->Update by schedule (Maybe not mandatory, however, I did this as sometimes it takes a lot of time to save a category with this). Then Run : php bin/magento indexer:reindex
  4. Admin-> Configuration->Catalog->Catalog->Storefront->Change the Use Flat Catalog Category => No (Mandatory)
  5. Admin-> Configuration->Catalog->Catalog->Storefront->Change the Use Flat Catalog Product => No (Mandatory)
  6. Install this module Regenerate Url rewrites by olegkoval

  7. Run bin/magento ok:urlrewrites:regenerate --entity-type=category (Better to run as a background process by adding '&' to at the end of the command)

  8. Run bin/magento ok:urlrewrites:regenerate --entity-type=product (Better to run as a background process by adding '&' to at the end of the command) (Optionally you can run bin/magento ok:urlrewrites:regenerate in one step)
  9. Change back the Change the Use Flat Catalog Category => Yes
  10. Change back the Change the Use Flat Use Flat Catalog Product => Yes

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:

bin/magento ok:urlrewrites:regenerate
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top