Magento How to remove categories from product URLS on the sitemap?

Magento How to remove categories from product URLS on the sitemap?

September 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento How to remove categories from product URLS on the sitemap?

Removing Categories From Magento Product URLs on Sitemaps

Magento’s sitemap generation process can sometimes include category paths in product URLs that you may not want to appear. This often happens even after disabling the “Use Categories Path for Product URLs” setting in the backend configuration.

If you’re facing this issue and need to remove categories from your XML sitemap, follow these steps:

Understanding the Issue

When Magento generates a sitemap, it typically uses the getUrlPath() method of the category model. This method includes the category path information by default, even if the “Use Categories Path for Product URLs” setting is turned off.

The default behavior looks something like this (simplified):

$category = $block->getCategory();
$urlKey = $category->getUrlPath();

return $this->_urlBuilder->getUrl($urlKey);

This code generates a URL that includes the category path, such as /category1/category2/product.

Solution: Override the Sitemap Generation Process

To resolve this issue without manually editing each URL, you’ll need to override Magento’s sitemap generation process. Specifically, you should create an event observer for the controller_action_predispatch_catalog_sitemap_index event.

Here’s a step-by-step guide:

  1. Create a new module: Create a custom module using composer or manually in your project’s app/code directory. For this example, let’s call it “Remove_Category_From_Sitemap”.

  2. Override the Sitemap Controller

    In your module’s etc/events.xml file, declare an observer for the mentioned event:

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="controller_action_predispatch_catalog_sitemap_index">
            <observer instance="Remove_Category_From_Sitemap\Observer\SitemapUrlModifier" />
        </event>
    </config>
    
  3. Create the Observer Class

    In your module’s Observer directory, create a SitemapUrlModifier class that extends \Magento\Framework\Event\Observer:

    namespace Remove_Category_From_Sitemap\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;
    use Magento\Framework\Controller\ResultFactory;
    
    class SitemapUrlModifier implements ObserverInterface
    {
        protected $_urlBuilder;
        protected $_request;
    
        public function __construct(
            \Magento\Framework\UrlInterface $urlBuilder,
            RequestInterface $request
        ) {
            $this->_urlBuilder = $urlBuilder;
            $this->_request = $request;
        }
    
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            if ($this->_request->getFullActionName() === 'catalog_sitemap_index') {
                $controller = $observer->getController();
                $resultPage = $controller->pageFactory->create();
    
                // Modify the URL key
                $urlKey = $controller->getRequest()->getParam('key');
                $modifiedUrl = $this->_urlBuilder->getUrl($urlKey);
    
                // Set the modified URL to the page
                $resultPage->setData('url', $modifiedUrl);
            }
    
            return $this;
        }
    }
    
  4. Clear Cache and Regenerate Sitemap

    After setting up your custom module, clear Magento’s cache and regenerate the sitemap using the backend interface.

Conclusion

Overriding Magento’s event-driven process is the most efficient way to remove categories from product URLs on generated sitemaps. This approach avoids manual URL editing for each product and ensures that your sitemap conforms to your desired URL structure.

For more information about Magento event observers, refer to the official Magento Developer Documentation.

Remember to always test changes thoroughly in a development environment before deploying them to a live site. If you need help with custom module development or other Magento-related tasks, feel free to contact our team at Laravel Company.

Catalog Cache Security Extensions

Our network