3

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.

0

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.

  • Are you sure about this? I know it is an accepted answer, but for me it does not work -_- Also the generate functions' docblock says Generate list based on current url rewrites which does not seem what we want here (Magento 2.2.5) – Alex Sep 3 '18 at 13:42
2

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
  • 1
    This is only for products as far as I can see. – Cudos May 30 '17 at 19:21
  • @Cudos I recently used it, it is for all products and categories – Kalyan Chakravarthi V Oct 11 '18 at 9:12
2

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]
1

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.

0

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
  • I ran this on a database with no entries in the url_rewrite table and I got the URL key already exists error. – mlunt Mar 14 at 0:31
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.