I was working on a Magento site this week and trying to clean up some URL problems that were causing some SEO headaches. I ended up adding an Observer for catalog_product_save_before to make some changes to the product URL prior to saving it. However, whenever I changed the URL, a new URL Rewrite was added for the old URL. For my purposes, this was not needed and might cause problems down the road and so I began looking for a way to disable this feature.
If you are editing the product from Magentos Adminhtml backend, you can just uncheck the ‘Create Permanent Redirect for old URL‘ checkbox when changing the products URL Key. However, since I was editing the product programatically via PHP, it was necessary to find a different solution.
On the admin edit product page, the checkbox’s name is product[url_key_create_redirect] and so I tried setting it like this: $product->setUrlKeyCreateRedirect(null);
However, that didn’t work. Fortuantly, Magneto is Open Source, so I was able to do take a peek at the code and see what is going on in the background. In the Adminhtml ProductController.php file, turns out, this is set using the field ‘save_rewrites_history‘, I found the following in the _initProductSave function:
/** * Create Permanent Redirect for old URL key */ if ($product->getId() && isset($productData['url_key_create_redirect'])) // && $product->getOrigData('url_key') != $product->getData('url_key') { $product->setData('save_rewrites_history', (bool)$productData['url_key_create_redirect']); }
So, while saving the product, I disabled this by doing the following:
$product->setSaveRewritesHistory(null);
Add a Comment