I have followed this tutorial Add a static URL prefix for product and category Magento 2

to create custom prefix for my store but i dont know why this is not working in my case

I am sharing all the details here :


<?xml version="1.0"?>   
  <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator" type="Demo\Mod\Model\CatalogUrlRewrite\ProductUrlPathGenerator"/>

    <type name="Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator">
      <plugin name="my_category_url_path_generator" type="Demo\Mod\Plugin\Model\CategoryUrlPathGenerator" sortOrder="15"/>



    namespace Demo\Mod\Model\CatalogUrlRewrite;

    class ProductUrlPathGenerator extends \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator

        const PRODUCT_PREFIX_ROUTE = 'product';
        const CATEGORY_PREFIX_ROUTE = 'product-category';
        public function __construct(
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
            \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
            \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
        ) {
            parent::__construct($storeManager, $scopeConfig, $categoryUrlPathGenerator, $productRepository);

        public function getUrlPath($product, $category = null)
            $path = $product->getData('url_path');
            if ($path === null) {
                $path = $product->getUrlKey()
                    ? $this->prepareProductUrlKey($product)
                    : $this->prepareProductDefaultUrlKey($product);

            if ($category !== null) {
                $categoryUrl = str_replace(self::CATEGORY_PREFIX_ROUTE .'/','',$this->categoryUrlPathGenerator->getUrlPath($category));
                $path = $categoryUrl . '/' . $path;

            return self::PRODUCT_PREFIX_ROUTE . '/' . $path;


namespace Demo\Mod\Plugin\Model;

class CategoryUrlPathGenerator

    public function afterGetUrlPath(\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $subject, $path)
        if (strpos($path, \Demo\Mod\Model\CatalogUrlRewrite\ProductUrlPathGenerator::CATEGORY_PREFIX_ROUTE) === false)
            $path = \Demo\Mod\Model\CatalogUrlRewrite\ProductUrlPathGenerator::CATEGORY_PREFIX_ROUTE . $path;

        return $path;

also i want to remove .html from url end. can i get some help here thanks in advance


You can remove .html from product url by following steps:

  1. Login to admin
  2. Goto Stores -> Configuration
  3. Then Navigate to Catalog -> Catalog
  4. Now move to Search Engine Optimization section
  5. Remove the .html from Product URL Suffix
  6. Save Config and Flush the cache

You can also do the same thing for Category url suffix as well. You can find the option there as well.

Updated Answer for Category and Product Url prefix:

You need regenerate the urls again for your website. You can do it by installing below module: https://github.com/olegkoval/magento2-regenerate_url_rewrites

You can follow the above guidelines after installation of the module.

If still not work then delete the urls for category and products by running following commands on your database.

Delete * FROM `url_rewrite` WHERE `entity_type` LIKE '%category%';
Delete * FROM `url_rewrite` WHERE `entity_type` LIKE '%product%';

Then run the commands for url regeneration of the above module.


  • but am not able to change url static prefix can you explain me how can i change this – Pramod Jan 31 '20 at 7:53
  • Wait I am updating my answer. – Sukumar Gorai Jan 31 '20 at 7:55
  • okay thank you am getting a tough time to understand this code since am a beginner in magento – Pramod Jan 31 '20 at 8:00

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.