Woocommerce 404 error on category pages [FIXED]

Creating a perfect url structure can be pretty difficult with WooCommerce.

Download plugin

Solve 404 error caused by same shop base and category base conflict

Creating a perfect url structure can be pretty difficult with WooCommerce, specially if you try to set the same shop base and category base slug. This situation makes permalink structure conflict as WordPress is confused and doesn’t know which layout to generate: single-product or product-category page.

If you try to set up WooCommerce permalinks to get something like:

  • example.com/shop-name/ => Shop page listing products
  • example.com/shop-name/category-name/ => Category page listing products from a category (generates 404 error without this plugin)
  • example.com/shop-name/category-name/product-name/ => Product page showing product details

Setting the permalink structure this way is SEO friendly and sometimes your client may demand it, but you should avoid generating conflicts in code!

How to fix Woocommerce 404 error on category pages

Here are the permalink settings that we use to generate the ultimate url structure:

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

ultimate woo url structure

Links generated:

www.site.com/my-shop/
=> Shop page listing products

www.site.com/my-shop/my-cat-1/
=> Category page listing products from a category (top-level category – 404 error)

www.site.com/my-shop/my-cat-1/sub-cat1/
=> 2nd level Category page(sub-category – 404 error)

www.site.com/my-shop/my-cat-1/sub-cat1/sub-sub-cat/
=> 3th level Category page(sub-sub-category – 404 error)

www.site.com/my-shop/my-cat-1/sub-cat1/sub-sub-cat/my-prod-3/
=> Single product from 3th level Category

Solution

So, we have to find a way to help WordPress distinguish categories from products.

Problem starts here www.site.com/my-shop/my-cat-1/ as obviously WordPress doesn’t know if “my-cat-1” is a product or category.

WP_Rewrite is WordPress’ class for managing the rewrite rules that allow you to use Pretty Permalinks feature. All rewrite rules are saved in database and all we have to do is to create a new rules for our site structure. We will not use general Regex syntax, but we will add a separate rewrite rule for every of our product categories.

This way we will tell WordPress how to resolve every single Product Category and this way we will avoid URL conflicts.

We made a plugin to work for you the way described. All you have to do is to upload and activate plugin. It will check if base and category structure are the same and will generate rewrite rule for all your Product Categories.

You can see on Status Page what’s done:

Simple as that!

Download plugin

Solve 404 error caused by same shop base and category base conflict

3 Comments. Leave new

Perfect fix, does exactly what WooCommerce should support in the first place!

Reply

Just perfect

Reply
Paul Richards
June 18, 2021 2:47 pm

Worked like a charm. Thanks!

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *