Magento 2 404 Error – Product Page 404 Error Caused by URL Rewrite Not Working

404magento2url-rewrite

We have a 404 issue on all product pages. Category pages showing fine as they should. Having imported circa 4000 products directly into our Magento2 store database and uploading images directly to the media folder, I realise we have omitted the correct steps for Magento2 in that we have not used the inbuilt import function. Thus the URL rewrite function has not ran.

When we create a new product manually from the Magento admin, the same issue occurs though. We have a 404 error in the frontend for the URL that is created. We can see the URL's are creating in the catalog_url_rewrite_product_category and url_rewrite tables.

We have re-ran below list of queries on command line to try to rectify:

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy en_US en_GB

php bin/magento setup:upgrade

php bin/magento cache:clean

php bin/magento cache:flush

And reset permissions as per below guide:

Magento 2 folder/file permissions

We are running the latest version of Magento 2.1.6 having upgraded recently to see if this solves the issue.

My feeling is that there is something corrupt in the database, but unsure how to debug further. And/or it's a permission issue as our cron is not running currently too which is another issue we are looking to fix today.

Any help and guidance would be greatly appreciated. Thanks.

Best Answer

I've worked out how to fix this for our particular situation. It was a corrupt database table issue. So error from system.log file relevant is below:

main.CRITICAL: PDOException: SQLSTATE[23000]: Integrity constraint violation: 
1452 Cannot add or update a child row: a foreign key constraint fails 
(`gamefish_mag2`.`report_event`, CONSTRAINT `REPORT_EVENT_EVENT_TYPE_ID_REPORT_EVENT_TYPES_EVENT_TYPE_ID` FOREIGN KEY (`event_type_id`) 
REFERENCES `report_event_types` (`event_type_id`) ON DELE) 
in /var/www/vhosts/domain.com/httpdocs/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228

Restoring the report_event table to a previous version before the migration of product content from Squarespace from a month or so back did the trick. This product the product URL back to life and allowed us to add products to the cart again. Hope this helps somebody else one day for similar issue.

Related Topic