Magento 2, a powerful eCommerce platform, provides flexible URL structure configurations, but by default, it includes the parent category in the URL for subcategories. This can create longer, less clean URLs for your products and subcategories. For example, if you have a parent category called “Electronics” and a subcategory called “Laptops,” the default URL might look like:
www.yoursite.com/electronics/laptops
However, you may prefer a cleaner URL structure where the parent category is excluded, and the subcategory URL is as simple as:
www.yoursite.com/laptops
Removing the parent category from subcategory URLs can make your website’s URLs cleaner, more user-friendly, and potentially better for SEO. This guide will walk you through how to remove the parent category from subcategory URLs in Magento 2.
Why Remove Parent Categories from URLs?
Before we jump into the technical process, it’s worth understanding why removing the parent category might be beneficial:
- Cleaner and Shorter URLs: This improves readability and is easier for customers to remember.
- SEO Optimization: Shorter URLs focusing on the subcategory keyword could be more SEO-friendly, especially if the parent category is generic and not critical to the subcategory's content.
- Improved User Experience: A simpler URL structure may improve the click-through rate, leading to better site navigation and user engagement.
Magento 2 does not offer an out-of-the-box feature to remove parent categories from subcategory URLs, but there are ways to achieve this by tweaking the URL rewrite system and using custom code. Below are the steps to remove parent categories from subcategory URLs in Magento 2.
Method 1: Modify URL Structure via Admin Panel
Magento 2 offers the option to configure how category URLs are generated in the admin panel. However, removing the parent category from the URL involves custom code, which we’ll cover in the next method.
-
Log into Magento Admin Panel:
- Open your Magento 2 Admin Panel.
-
Go to Configuration Settings:
- Navigate to Stores > Configuration > Catalog > Search Engine Optimization.
-
Adjust Category URL Settings:
- Find the Category URL Suffix field and remove any suffix (e.g., “.html”) if it is present.
- Under Use Categories Path for Product URLs, set this option to No.
- Save the configuration.
Note: This method won’t completely remove the parent category from URLs but will stop Magento from appending the entire category path for product URLs.
Method 2: Custom Code to Remove Parent Category from Subcategory URLs
For more control, you can use custom code to rewrite the URLs. This involves editing your theme or creating a small module to modify the default URL structure for subcategories.
Here are the steps to remove the parent category from subcategory URLs in Magento 2 using custom code:
Step 1: Create a Custom Module
-
Create a Module Folder:
- In your Magento root directory, create the following folder structure:
-
Create the
registration.php
File:- Inside the
app/code/Namespace/RemoveParentCategory
folder, create theregistration.php
file and add the following content: -
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register( ComponentRegistrar::MODULE, 'Namespace_RemoveParentCategory', __DIR__ );
- Inside the
-
Create the
module.xml
File:- In the
app/code/Namespace/RemoveParentCategory/etc
folder, create themodule.xml
file and add the following content: -
<?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="Namespace_RemoveParentCategory" setup_version="1.0.0"/> </config>
- In the
-
Step 2: Create a Plugin to Modify Category URLs
-
Create the Plugin File:
-
In the
app/code/Namespace/RemoveParentCategory/Plugin
folder (create the folder if it doesn't exist), create a PHP file calledCategoryUrlPlugin.php
and add the following content: -
<?php namespace Namespace\RemoveParentCategory\Plugin; use Magento\Catalog\Model\Category; use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator; class CategoryUrlPlugin { /** * @param CategoryUrlPathGenerator $subject * @param string $result * @param Category $category * @return string */ public function afterGetUrlPath(CategoryUrlPathGenerator $subject, $result, Category $category) { // Get the category path (category hierarchy) $categoryPath = $category->getUrlPath(); // Remove the parent category from the URL path $categoryParts = explode('/', $categoryPath); if (count($categoryParts) > 1) { array_shift($categoryParts); // Remove the first (parent) category part } return implode('/', $categoryParts); } } This plugin will hook into the category URL generation and remove the parent category from the URL.
-
Enable the Module:
- Run the following commands from your Magento root directory to enable your new module:
- php bin/magento module:enable Namespace_RemoveParentCategory
php bin/magento setup:upgrade
php bin/magento cache:flush
-
-
After implementing the custom module, clear your cache and reindex your Magento store. Then, check your subcategory URLs to ensure that the parent category is no longer included in the URL.