avatar Deluxe Blog Tips About Projects

How to set product category base the same as shop base in WooCommerce

I've been searching in the Internet for guide how to fix the 404 error when I set product category base the same as shop base in WooCommerce and unfortunately there's no ultimate solution that works for me. So I decided to work on that myself and here is my solution:

A quick brief of the problem: in Settings \ Permalinks, if I set:

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

then when visit each of the following pages, I get the following result:

  • Shop: works
  • Product category base: 404 error
  • Product permalink base: works

To fix that 404 error, I use the snippet below (you can insert into functions.php file or better - in a plugin):

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;
} );

This simply adds a new rewrite rules for product category. This rewrite rule sits on top of all WordPress rewrite rules, so it takes priority to be performed first.

Hope the small snippet can help you fix the unexpected 404 error, which took me 2 hours looking for solution!

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com