4

I have tried to regenerate urls for categories and products after migration from Magento 1.9.x to Magento 2.2.x through following modules:

  1. Url Regenerator 1
  2. Url Regenerator 2

These 2 generates the urls for categories and products but in frontend, I am getting 404 error when click on products from Homepage, search result page, any widgets etc. Product urls works fine when going from category pages but not for all categories. From some of the categories its shows 404.

Good thing is that all category urls are working perfectly. But not the product urls.

I have multiple stores. Anyone have the same issues?

4
  • are you sure that you do not have any duplicate url_keys in your shop, maybe for some store views? For products that give a 404: does the same product give a 404 in every store view, or different products in different store views, that work in another one? Commented Oct 15, 2018 at 6:08
  • also, what is your setting for "use category in product URLs"? Commented Oct 15, 2018 at 6:08
  • I have already truncate the tables Commented Oct 15, 2018 at 6:10
  • No I am not using use category path in product urls. Commented Oct 15, 2018 at 6:11

2 Answers 2

0

Truncate all product/category urls from url_rewrite table and then regenerate them by saving a parent cateogory (after modifying either is_anchor or url_key field).

3
  • I have 60 parent category and around 1800 sub categories. It's taking around 10 mins to save one category so it's not a solution. Commented Oct 14, 2018 at 19:24
  • try saving only the root category. Commented Oct 16, 2018 at 15:08
  • Tried but no luck. Commented Oct 16, 2018 at 18:36
0

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.

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:

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.

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 Hope that helps!

7
  • 1
    If you check my question, then you will find that I have already tried the plug-in you are asking. Commented Oct 15, 2018 at 5:22
  • I have shared 2 module link github.com/Iazel/magento2-regenurl and github.com/olegkoval/magento2-regenerate_url_rewrites and possible problem and process of magento 1 & magento 2 for reganrate URL. so you can verify it. Commented Oct 15, 2018 at 5:29
  • Because you faced this problem because your regenerate URL not working properly. Commented Oct 15, 2018 at 5:30
  • Can you check my question, I have already tried these modules. But no luck. Commented Oct 15, 2018 at 5:30
  • I am creating my own module so far its working fine for products. Working with categories. Commented Oct 15, 2018 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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