Magento – Magento 2.2.4 The value specified in the URL Key field would generate a URL that already exists

admincategorymagento2magento2.2.4url-rewrite

when I have the category in product assign and saving category below error showing

The value specified in the URL Key field would generate a URL that
already exists.

To resolve this conflict, you can either change the
value of the URL Key field (located in the Search Engine Optimization
section) to a unique value or change the Request Path fields in all
locations listed below:

enter image description here

and my exception.log error log is

enter image description here

Best Answer

There are so may issue is already reported on stack & GitHub and other community. and there are so many solutions for that and which one is correct it is to difficult choosing the right one. but I see one issue is reported on GitHub which one help to you I think. https://github.com/magento/magento2/issues/7298

This issue occurring due to duplicate entry on the database table url_rewrite, so there is only one way to resolve this issue you need to empty the table and regenerate the URL. so I have checked and found one answer which one usefull to me which I shared below.


Note: Take database backup before doing below opration.

Remove the duplication data when saving category. This data is throw in method doReplace($urls) in \vendor\magento\module-url-rewrite\Model\Storage\DbStorage.php file.

protected function doReplace($urls)
{
    foreach ($this->createFilterDataBasedOnUrls($urls) as $type => $urlData) {
        $urlData[UrlRewrite::ENTITY_TYPE] = $type;
        $this->deleteByData($urlData);
    }
    $data = [];
    foreach ($urls as $url) {
        $data[] = $url->toArray();
    }
    $this->insertMultiple($data);
}

I found out $data variable has a duplicate record. If you want this method to work without any errors. Rewrite this method above to below one.

protected function doReplace($urls) {
        foreach ($this->createFilterDataBasedOnUrls($urls) as $type => $urlData) {
            $urlData[UrlRewrite::ENTITY_TYPE] = $type;
            $this->deleteByData($urlData);
        }
        $data = [];
        $storeId_requestPaths = [];
        foreach ($urls as $url) {
            $storeId = $url->getStoreId();
            $requestPath = $url->getRequestPath();
            // Skip if is exist in the database
            $sql = "SELECT * FROM url_rewrite where store_id = $storeId and request_path = '$requestPath'";
            $exists = $this->connection->fetchOne($sql);

            if ($exists) continue;

            $storeId_requestPaths[] = $storeId . '-' . $requestPath;
            $data[] = $url->toArray();
        }

        // Remove duplication data;
        $n = count($storeId_requestPaths);
        for ($i = 0; $i < $n - 1; $i++) {
            for ($j = $i + 1; $j < $n; $j++) {
                if ($storeId_requestPaths[$i] == $storeId_requestPaths[$j]) {
                    unset($data[$j]);
                }
            }
        }
        $this->insertMultiple($data); 
}

Taje refrance from Magento 2 Duplicate Product URLs Problem And "URL key for specified store already exists." cannot save category #7298 I hope it helps!

Related Topic