To remove category paths from product url set "Use Categories Path for Product URLs"
to "No"
under System -> Configuration -> Catalog -> Search Engine Optimizations
.
To remove parent category path from child category url
Rewrite class Mage_Catalog_Model_Url
override method public function getCategoryRequestPath($category, $parentPath)
like below
public function getCategoryRequestPath($category, $parentPath)
{
$storeId = $category->getStoreId();
$idPath = $this->generatePath('id', null, $category);
$suffix = $this->getCategoryUrlSuffix($storeId);
if (isset($this->_rewrites[$idPath])) {
$this->_rewrite = $this->_rewrites[$idPath];
$existingRequestPath = $this->_rewrites[$idPath]->getRequestPath();
}
if ($category->getUrlKey() == '') {
$urlKey = $this->getCategoryModel()->formatUrlKey($category->getName());
}
else {
$urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey());
}
$categoryUrlSuffix = $this->getCategoryUrlSuffix($category->getStoreId());
// Code commented to remove parent category path from child category path
//if (null === $parentPath) {
//$parentPath = $this->getResource()->getCategoryParentPath($category);
//}
//elseif ($parentPath == '/') {
$parentPath = '';
//}
$parentPath = Mage::helper('catalog/category')->getCategoryUrlPath($parentPath,
true, $category->getStoreId());
$requestPath = $parentPath . $urlKey . $categoryUrlSuffix;
if (isset($existingRequestPath) && $existingRequestPath == $requestPath . $suffix) {
return $existingRequestPath;
}
if ($this->_deleteOldTargetPath($requestPath, $idPath, $storeId)) {
return $requestPath;
}
return $this->getUnusedPath($category->getStoreId(), $requestPath,
$this->generatePath('id', null, $category)
);
}
Modified below code
if (null === $parentPath) {
$parentPath = $this->getResource()->getCategoryParentPath($category);
}
elseif ($parentPath == '/') {
$parentPath = '';
}
to
//if (null === $parentPath) {
//$parentPath = $this->getResource()->getCategoryParentPath($category);
//}
//elseif ($parentPath == '/') {
$parentPath = '';
//}
If you look at the class Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator
which is responsible for the category URL rewrite generation, you will see that it uses a constant value to determine if the parent path should be added:
protected function isNeedToGenerateUrlPathForParent($category)
{
return $category->isObjectNew() || $category->getLevel() >= self::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING;
}
The value is
const MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING = 3;
which means the path is generated for every category that is not a top category (level 1 is the root category) and you cannot configure this value or turn off parent path generation.
And since it is a protected method we cannot modify the return value with a plugin. Instead you will have to write a plugin for the public method getUrlPath()
that uses isNeedToGenerateUrlPathForParent()
.
I would use an around plugin which does not call the original method, essentially replacing the method:
class RemoveParentCategoryPathPlugin
{
public function aroundGetUrlPath($subject, $proceed, $category)
{
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;
}
}
(this is the original method, but without the parent path generation)
Plugin documentation: http://devdocs.magento.com/guides/v2.1/extension-dev-guide/plugins.html
Update existing categories
This will apply for new categories. Unfortunately the category URLs are not generated by an indexer anymore, so reindexing does not change them and the only way to regenerate them is to save each category. It could be done with a one-off script that loads a collection of all categories and calls save()
on it:
$allCategories = $categoryCollectionFactory->create()->load();
foreach ($allCategories as $category) {
$category->setDataChanges(true);
$category->save();
}
Note that this is not optimized for performance at all and will load all categories at once. So if you have thousands of categories you might want to process them in batches.
Best Answer
We can configure it by URL Rewrites
Go to Magento 2 Admin -> Marketing -> SEO & Search -> URL Rewrites
Find the path by searching
Request path
column.Remove the cache
php bin/magento cache:clean
.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
Modify
getUrlPath($category)
by Magento Plug-in feature.Hope this helps.