The products we sell are unique, but share the same name. So when we add two different products with the same name, it throws back the error that that URL already exists.

What I wanted to do was add the SKU to the URL key. That way it would always have a unique value. But I don't want to have to manually add it each time.

Is there a way to add this in the back end? Similar to the Configuration/Catalog/Catalog section allows for adding the {{sku}} to the meta tags etc?

2 Answers 2


I need the same behaviour, i need to have product's sku at the end of product's url, because many products have same name.

So, i created a custom module to override /vendor/magento/module-catalog-url-rewrite/Model/ProductUrlPathGenerator.php where in prepareProductUrlKey() i changed

return $product->formatUrlKey($urlKey === '' || $urlKey === null ? $product->getName() : $urlKey);


return $product->formatUrlKey($urlKey === '' || $urlKey === null ? $product->getName() . "-" . $product->getSku() : $urlKey);

with product's sku added at the url key end.


Based on LucScu answer I created my own module wich can be helpful to you as well, the file essential to it are the di.xml inside app/code/Developer/Module/etc

<?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\ProductUrlPathGenerator" type="Developer\Module\Model\ProductUrlPathGenerator" />

and ProductUrlPathGenerator.php inside app/code/Developer/Module/model

namespace FivePreview\URLRewrite\Model;

 * Class AbstractAction
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)

class ProductUrlPathGenerator extends \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator {

    protected function prepareProductDefaultUrlKey(\Magento\Catalog\Model\Product $product)
        $storedProduct = $this->productRepository->getById($product->getId());
        $storedUrlKey = $storedProduct->getUrlKey();
        return $storedUrlKey ?: $product->formatUrlKey($storedProduct->getName() . "-" . $storedProduct->getSku());

    protected function prepareProductUrlKey(\Magento\Catalog\Model\Product $product)
        $urlKey = $product->getUrlKey();
        return $product->formatUrlKey($urlKey === '' || $urlKey === null ? $product->getName() . "-" . $product->getSku() : $urlKey);


and then of course you have to add the registration.php in app/code/Developer/Module and the module.xml inside app/code/Developer/Module/etc if you want it to disable/enable

Your Answer

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

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