Magento 2: How to renew the category URL rewrites?
-
14-04-2021 - |
質問
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.
解決
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.
他のヒント
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]
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
- Backup the database
- Run below in the database
delete from catalog_url_rewrite_product_category; delete from url_rewrite where entity_type='category' or entity_type='product';
- 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
- Admin-> Configuration->Catalog->Catalog->Storefront->Change the Use Flat Catalog Category => No (Mandatory)
- Admin-> Configuration->Catalog->Catalog->Storefront->Change the Use Flat Catalog Product => No (Mandatory)
Install this module Regenerate Url rewrites by olegkoval
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)
- 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)
- Change back the Change the Use Flat Catalog Category => Yes
- 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