How to Fix Base Page Product Permalink 404 Error

May 20, 2015 by Mark Ward in Miscellaneous, Quick Tips, Web Design, WooCommerce, WordPress

I found a solution to a very common issue with WooCommerce permalink structure.

A quick brief of the problem:

In Settings \ Permalinks, if you set:

  • Shop base: products
  • Product category base: products (same as shop base)
  • Product permalink base: Shop base with category, e.g. products/%product_cat%/

When you visit each of the following pages, you got the following result:

  • Shop Base: products works
  • Product category base: products/%product_name% 404 error
  • Product permalink base: products/%product_name% works

That pesky 404 error:

I search for several hours for a fix to this seemingly simple request. However, everything I tried wasn’t working for me. But, alas I found this solution.

The Fix:

What this code does:

This code snippet add url rewrite rule that will override the WordPress rewrite rules for WooCommerce product categories. Long story short, it just works.

Add this code to functions.php and update your permalinks.

Voila, problem solved.

You will now have this permalink structure:

  • Products: products works
  • Product category base: products/%product_cat% works
  • Product permalink base: products/%product_cat%/%product_name% works

Note: If your pages have been indexed using the old structure you should probably add redirects from the old links to the new structure.

Updating Permalinks and Existing Links

1.) Add redirects to .htaccess:

old:  /products/%product_name%/
new: /products/%product_cat%/%product_name%/

2). Perform a Search & Replace to update any hard-coded links to new permalink structure.

Tools to make your life easier:

Better Search and Replace (Plugin)

I would use this plugin for updating most things.

Search and Replace for WordPress Database (Install & Run from Website Root)

Use this if you need to perform search & replace on data that has been serialized. Some sliders such as Slider Revolution and  LayerSlider WP serialize their data, so a basic search & replace would miss these.

Hope this helps someone.

Tags: