Magento 2 troubleshooting › Catalogue
Why does a Magento product URL return 404?
Short answer: Magento serves product pages through url_rewrite. If the product has no row there for that store view, its URL 404s even though the product is fine. This happens after imports, website changes and direct database edits.
When a product is saved, Magento writes one url_rewrite row per store view (and per category path, if enabled). Products changed without a save (imports with some tools, SQL updates, website assignment by script) can end up with none.
Check it step by step
These queries only read data. Run them on a copy of your database if you can, or ask your developer to. If your tables have a prefix (set in app/etc/env.php), add it to each table name.
1. Does this product have rewrites?
Works on Magento Open Source and Adobe Commerce:
SET @sku = 'ABC-123'; -- your SKU
SELECT ur.store_id, ur.request_path, ur.target_path, ur.redirect_type
FROM url_rewrite ur
JOIN catalog_product_entity e ON e.entity_id = ur.entity_id
WHERE ur.entity_type = 'product'
AND e.sku = @sku
ORDER BY ur.store_id, ur.request_path;
No rows for a store view means a 404 there. A row with redirect_type 301 is a redirect, not the page itself.
2. Every visible product with no rewrite in a store view of its website
Magento Open Source
SELECT e.sku, s.code AS store_view
FROM catalog_product_entity e
JOIN catalog_product_website pw ON pw.product_id = e.entity_id
JOIN store s ON s.website_id = pw.website_id AND s.store_id > 0
JOIN eav_entity_type t ON t.entity_type_code = 'catalog_product'
JOIN eav_attribute a_visibility ON a_visibility.entity_type_id = t.entity_type_id AND a_visibility.attribute_code = 'visibility'
JOIN catalog_product_entity_int vis ON vis.entity_id = e.entity_id AND vis.attribute_id = a_visibility.attribute_id AND vis.store_id = 0 AND vis.value IN (2, 3, 4)
WHERE NOT EXISTS (
SELECT 1 FROM url_rewrite ur
WHERE ur.entity_type = 'product' AND ur.entity_id = e.entity_id
AND ur.store_id = s.store_id AND ur.redirect_type = 0
)
ORDER BY e.sku, s.code
LIMIT 200;
Adobe Commerce
Product, category, rule and CMS values are stored per version and joined on row_id (why):
SELECT e.sku, s.code AS store_view
FROM catalog_product_entity e
JOIN catalog_product_website pw ON pw.product_id = e.entity_id
JOIN store s ON s.website_id = pw.website_id AND s.store_id > 0
JOIN eav_entity_type t ON t.entity_type_code = 'catalog_product'
JOIN eav_attribute a_visibility ON a_visibility.entity_type_id = t.entity_type_id AND a_visibility.attribute_code = 'visibility'
JOIN catalog_product_entity_int vis ON vis.row_id = e.row_id AND vis.attribute_id = a_visibility.attribute_id AND vis.store_id = 0 AND vis.value IN (2, 3, 4)
WHERE NOT EXISTS (
SELECT 1 FROM url_rewrite ur
WHERE ur.entity_type = 'product' AND ur.entity_id = e.entity_id
AND ur.store_id = s.store_id AND ur.redirect_type = 0
)
AND e.created_in <= UNIX_TIMESTAMP() AND e.updated_in > UNIX_TIMESTAMP()
ORDER BY e.sku, s.code
LIMIT 200;
How to fix it
- Saving the product in the admin regenerates its rewrites. For a few products, open each and click Save.
- For many products, Magento has no built-in command; developers usually use a URL-regeneration CLI module, or re-import the products with their
url_key. - If saving fails with “URL key for specified store already exists”, another product, category or CMS page owns that path; see duplicate URL key.
Good to know
url_rewrite.entity_idis the product'sentity_idon both Open Source and Adobe Commerce.