Skip to main content

A client recently had issues with category URLs being suffixed with numbers that couldn’t be removed. This stemmed from thousands of URL rewrites that were likely generated by flawed scripts. The website also had multiple store views despite only ever being a single site.

Magento Product suffix issues

Product-name-2, Product-name-3 etc could be found throughout the URL_rewrite table. There were too many to remove individually, and I was concerned at the repercussions of removing bulk URL rewrites.

Magento Product URL issues

URL key for specified store already exists

‘URL key for specified store already exists’ is also a common error that may require you to regenerate all product / category URLs.

Magento’s URL rewrites are only editable individually, so it’s impossible to regenerate them all without a plugin or script.

Magento 2 – regenerating URL rewrites manually

Delete all Magento URL rewrites with in the database within PHPMyAdmin. Backup and run this on staging instance.

delete from url_rewrite;
delete from catalog_url_rewrite_product_category;

Truncating these tables would require you to disable foreign key checks.

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE url_rewrite;
SET FOREIGN_KEY_CHECKS = 1;

Once all URL rewrites are removed the website will lose the SEO friendly URLs and instead have catalog/number paths e.g. catalog/product/view/id/1/category/2. To get your site URLs back to a more usable state you’ll need to regenerate them.

Some users state that reindexing or altering the URL of a root category will cause Magento to automatically regenerate all your URLs.

This wasn’t the case for me, so I had to use a plugin / script.

website code

Magento 2 – regenerating URL rewrites with a plugin

A few popular plugins are now deprecated, so I opted for olegkoval’s magento2 regenerate url rewrites.

olegkoval – magento2 regenerate url rewrites

The plugin is easy to install via composer from it’s GIT repo. It also seems to offer the option to delete the URLs writes before regenerating them, so in theory step 1 can be skipped. Backup and run this on staging instance.

Install

composer require olegkoval/magento2-regenerate-url-rewrites

Enable

php bin/magento module:enable OlegKoval_RegenerateUrlRewrites

Regenerate for products & categories (default store view)

php bin/magento ok:urlrewrites:regenerate --entity-type=product
php bin/magento ok:urlrewrites:regenerate --entity-type=category

Magento 2 Search Terms

Although regenerating URL rewrites helped to clean up the product and category URL suffixes, I still found that the search was inaccurate and had broken links. I rectified this by deleting all the stored search terms. Site searches now suggest the correct number of products and no longer have broken redirects.

Magento search terms - redirect table

This and the URL rewrite work seems to have cleaned up the search accuracy problems that the client had been experiencing for some time.

Andrew Taylor

A senior UI designer with over 25 years of web design and web development experience working for some of the largest companies in the UK. An expert in all things Magento and WordPress.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.