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.

2 Answers 2


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) ){

                                        $firstCategoryId = $cats[1];
                                          $firstCategoryId = $cats[0];

                            $_category = $_objectManager->create('Magento\Catalog\Model\Category')->load($firstCategoryId);
                            $categorykey = $_category->getUrlKey();
                            $siteurl= $block->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);      
  • 3
    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, 2019 at 10:47
  • chanchal did you find any solution for this? Jul 20, 2021 at 9:02

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 and acknowledge you have read our privacy policy.

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