1

In Magento 2, when I try to change URL key for a category, I get this message:"URL key for specified store already exists.". This is my code:

$this->logger->log("id is: " . $c->getId(), true);
$c->load($c->getId());
$newUrlKey=$c->getUrlKey() . "-baba" . $count++;
$this->logger->log($newUrlKey, true);
$c->setUrlKey($newUrlKey);
$this->logger->log($c->getData(), true);

$c->save();

But I know there is no other category with this url key. What is the problem with Magento here?

  • Hi @vladimir-despotovic, I have a similar issue - when creating products. For me, it happens when the first url key I try to add exists. I catch the error and try to save the same product several times with a bunch of different url keys (including an incremental counter). This fails with the error message you quote. – where Nov 1 '18 at 21:46
1

The error is due to url_key on the category you are trying to save. Most probably there is some rewrite with the same key for the category link added as redirect. Check url_rewrite table for rows with request_path containing the url_key of the category you are trying to save.

  • the problem is that this is the root category and it looks like this error always raises, no matter which url key I choose. – Vladimir Despotovic Apr 20 '17 at 6:00
  • 1
    Looks like this error is unavoidable. Yes, I tried to put a random name for the url key (jibble jabble word) and it disn't work(ie.error is raised again) – Vladimir Despotovic Apr 20 '17 at 6:01
  • 1
    Because there is some old redirect in url_rewrite table. Dump url_rewrite table to a backup file and clear it. Then try to save the category. It should work. I think the problem occurs when you have multiple store, sets url_key first for default which creates rewrites for all stores and then changes it in the store scope without unchecking "keep the old redirect" which creates a rewrite from default name to the current one as 301. – Zefiryn Apr 20 '17 at 6:06
  • 1
    Unfortunately, this didn't solve the probem. I deleted everything from url_rewrite, and the problem appears on root and also on another category. I didn't notice until now. – Vladimir Despotovic Apr 20 '17 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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