0

I am using magento-2.3 EE. I want to include category path in product url, in sitemap urls and same should be in their canonical urls.

Like in sitemap, product urls should be like - site.com/category1/category2/product-name.html

Similar structure should be in canonical urls of each product.

0

Try with below step to do with category path :

Admin -> Stores -> Configuration -> Catalog -> Catalog -> Search Engine Optimization -> "Use Categories Path for Product URLs" set to "Yes"

if you want using pragmatically than try with below code :

<?php $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?>
                        <?php     $cats = $_item->getCategoryIds();
                                if(count($cats) ){

                                    if($cats[0]==2){
                                        $firstCategoryId = $cats[1];
                                    }
                                    else{
                                          $firstCategoryId = $cats[0];
                                    }

                            $_category = $_objectManager->create('Magento\Catalog\Model\Category')->load($firstCategoryId);
                            $categorykey = $_category->getUrlKey();
                            $siteurl= $block->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);      
                            $pro_url=$siteurl.$categorykey."/".$_item->getUrlKey().".html";
                            }
                            else{
                                $pro_url=$block->getProductUrl($_item);
                            }
                     ?>
  • The above configuration is already set to yes. But still the category paths are not included in product urls in sitemap and canonical urls. – chanchal Sep 16 at 10:47
0

I suspect this cannot be done with Magento 2 out-of-the-box. You probably need to use some 3rd party extension.

Magento 2 always generates /product-url.html links as your product can be in multiple categories. So M2 omits the category part in the url.

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.