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:

add_filter( 'rewrite_rules_array', function( $rules ) { $new_rules = array( 'shop/([^/]*?)/page/([0-9]{1,})/?$' => 'index.php?product_cat=$matches[1]&paged=$matches[2]', 'shop/([^/]*?)/?$' => 'index.php?product_cat=$matches[1]', ); return $new_rules + $rules; } ); 

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%/

#permanent redirect example

redirect 301 /products/%product_name%/ /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.

=> 'index.php?product_cat=$matches[1]&paged=$matches[2]',  'products/([^/]*?)/?

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.

=> 'index.php?product_cat=$matches[1]',  );  return $new_rules + $rules; } );

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.