Magento 2 - How to remove parent categories slug from subcategory URLs to make it shorter?

We can configure it by URL Rewrites

  1. Go to Magento 2 Admin -> Marketing -> SEO & Search -> URL Rewrites

  2. Find the path by searching Request path column.

  3. Remove the cache php bin/magento cache:clean.

  4. Refresh the front-end, click the main menu category then you can see the configured path.

Example i have modified.

Request path men/tops-men/tees-men.htm

Modified path tees-men.htm

Or

You Can Build category URL path by

\vendor\magento\module-catalog-url-rewrite\Model\CategoryUrlPathGenerator.php 

Modify getUrlPath($category) by Magento Plug-in feature.

Hope this helps.


As pointed out by @Rajan Soni, you need to override the native Magento class :

  • vendor\magento\module-catalog-url-rewrite\Model\CategoryUrlPathGenerator.php

To do so, you need to use an existing module, or to create to a new dedicated one. You will need 4 files, located in your new module Vendor_Module directory :

  • app/code/Vendor/Module/
  • app/code/Vendor/Module/registration.php
  • app/code/Vendor/Module/etc/module.xml
  • app/code/Vendor/Module/etc/di.xml
  • app/code/Vendor/Module/Model/CategoryUrlPathGenerator.php

app/code/Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
        <sequence></sequence>
    </module>
</config>

app/code/Vendor/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

app/code/Vendor/Module/etc/di.xml

<?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\CategoryUrlPathGenerator" type="Vendor\Module\Model\CategoryUrlPathGenerator" />
</config>

app/code/Vendor/Module/Model/CategoryUrlPathGenerator.php

<?php

namespace Vendor\Module\Model;

use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator as Magento_CategoryUrlPathGenerator;

class CategoryUrlPathGenerator extends Magento_CategoryUrlPathGenerator {

    public function getUrlPath($category, $parentCategory = null)
    {
        if (in_array($category->getParentId(), [Category::ROOT_CATEGORY_ID, Category::TREE_ROOT_ID])) {
            return '';
        }
        $path = $category->getUrlPath();
        if ($path !== null && !$category->dataHasChangedFor('url_key') && !$category->dataHasChangedFor('parent_id')) {
            return $path;
        }
        $path = $category->getUrlKey();
        if ($path === false) {
            return $category->getUrlPath();
        }

        return $path;
    }    
}

Then as usual, you will probably need to refresh some stuff :

  • bin/magento module:enable Vendor_Module;
  • bin/magento cache:clean;
  • bin/magento setup:di:compile;
  • bin/magento setup:upgrade;
  • bin/magento setup:static-content:deploy -f;

As noticed above, you could also need to refresh "url_rewrite" table, and save / reindex categories.

PS : @Siarhey Uchukhlebau suggested to implement a fix with the plugin way, which I agree would be probably better than the "preference" solution provided, especially if you already had an extension which override this same class.

// I am pretty new to M2, so any suggestion about my post would be appreciated !

See ya


Sort and sweet solution for this is:

Just go to the file: \vendor\magento\module-catalog-url-rewrite\Model\CategoryUrlPathGenerator.php or Just override that file

In the getUrlPath() function just comment this code

/*if ($this->isNeedToGenerateUrlPathForParent($category)) {
        $parentPath = $this->getUrlPath(
            $this->categoryRepository->get($category->getParentId(), $category->getStoreId())
        );
        $parentPath ="";
        //$path = $parentPath === '' ? $path : $parentPath . '/' . $path;
        $path = $parentPath === '' ? $path : $path;
    }*/

And open the database find the table url_rewrite just backup it and truncate that table.

then, go to your admin > products > categories choose any one category and save that.

then check your frontend.

Your problem will be solve.