I want to remove the trailing category in magento 2


instead I want


3 Answers 3


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


You Can Build category URL path by


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

Hope this helps.

  • 1
    Using this I have to do it one by one. and when creating new category it again create nested url. any solution that do it directly when create new category or update category Jul 21, 2016 at 7:37
  • Have a look at the modified answer. Jul 21, 2016 at 9:22
  • thank that was helpful Jul 21, 2016 at 9:34

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


<?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">




<?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" />



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

  • Thanks, added as module github.com/magefast/CategoryNoParentPath
    – Alex
    Feb 7, 2022 at 12:03
  • This extension is useful when creating new sub categories but is doesn't change URLs of already created sub categories. Any idea how to remove parent path from already created categories? Mar 30, 2022 at 11:33

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.

  • It is not a good idea to make changes right in the core code. I recommend you to use a plugins instead. Apr 7, 2017 at 12:40
  • Brother, you can override that file.
    – Rajan Soni
    Apr 7, 2017 at 12:46
  • Sure, but you should edit your answer and add necessary info about that: how to overwrite whole class or how to create the around-plugin. Apr 7, 2017 at 12:50
  • Ya, I edit my answer and I mention in bold to override that file bro.
    – Rajan Soni
    Apr 7, 2017 at 12:58
  • You can down-vote many times, but it won't add mind for you Apr 7, 2017 at 13:36

Your Answer

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

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