Navigating Magento 2 URL Rewrite Chaos: When 'No Redirect' Means 'Yes!'

Managing product URLs and ensuring proper redirects is crucial for any e-commerce platform's SEO and user experience. In Magento 2, this process is generally robust, but a recent GitHub issue (#41825) highlights a complex set of bugs in Magento Open Source 2.4.9 (and potentially other versions) that can turn routine product management into a frustrating exercise in URL rewrite chaos.

This deep dive into the Magento 2 core reveals how the "Create Permanent Redirect for old URL" checkbox can be ignored, leading to unwanted 301 redirects, stale URL rewrites for invisible products, and even preventing new product duplications.

The Ignored Checkbox: When "No Redirect" Means "Yes"

One of the most perplexing issues reported is the system's disregard for the "Create Permanent Redirect for old URL" checkbox when an admin changes a product's URL key. Despite explicitly opting out of a 301 redirect, Magento 2.4.9 generates them anyway for stores where the product is visible. This happens because the system's global configuration setting for saving rewrite history (catalog/seo/save_rewrites_history) overrides the product-specific checkbox value during the URL rewrite generation process.

The issue author points to a specific line in Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator::generate():

$product->setData('save_rewrites_history', $isSaveHistory);

This line, introduced in a previous update, overwrites the admin's choice. A proposed fix involves conditionally setting this value:

$product->setData('save_rewrites_history', $product->getData('save_rewrites_history') ?? $isSaveHistory);

This ensures the explicit admin choice takes precedence, falling back to the global config only if no specific value is set.

Invisible Products, Visible Rewrites: A Hidden SEO Trap

Another significant problem arises during product duplication, particularly with products configured to be "Not Visible Individually" in specific store views. When a product is duplicated, Magento's Product Copier first saves it at a global scope. This triggers URL rewrite generation for all store views, even those where the product is supposed to be hidden. Later, when store-view-specific visibility overrides are applied, the initial, incorrect URL rewrites for the invisible product remain.

The system's rewrite cleanup logic (ProductProcessUrlRewriteSavingObserver::deleteObsoleteRewrites()) fails to remove these stale entries because it only acts when the default scope visibility is "Not Visible Individually," which isn't the case here. This leaves behind orphaned rewrites that can clutter the database and potentially cause SEO issues.

The Duplication Deadlock: "URL Key Already Exists"

The culmination of these issues leads to a critical failure: subsequent attempts to duplicate products can fail with an "URL Key already exists" error. This happens because Magento's URL key uniqueness check (within Copier::setDefaultUrl()) only inspects the catalog_product_entity_varchar table for existing URL keys. It completely ignores the url_rewrite table, where the unwanted 301 redirects (from Problem A) and stale direct rewrites (from Problem B) are still present.

Since the old URL key is still "taken" in the url_rewrite table by these lingering entries, the system incorrectly believes the new duplicate product's proposed URL key is a duplicate, halting the duplication process. The only workaround is manual deletion of these old rewrites, which is inefficient and prone to error.

Impact and Community Call to Action

These bugs collectively create significant administrative overhead, potential SEO penalties from unwanted 301s, and developer frustration. They highlight a complex interplay within Magento's core URL rewrite and product management logic, an area that has seen related issues in the past (e.g., #19293, #12412).

For Magento 2 merchants and developers, understanding these intricacies is vital. While awaiting an official patch, the detailed analysis and proposed code-level fixes provided in the GitHub issue offer valuable insights for potential workarounds or targeted patches. This issue underscores the importance of rigorous testing during Magento migrations and updates, and the continuous need for community collaboration to refine and stabilize the platform's core functionalities.

Start with the tools

Explore migration tools

See options, compare methods, and pick the path that fits your store.

Explore migration tools