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 productsexample.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 detailsSetting the permalink structure this way is SEO friendly and sometimes your client may demand it, but you should avoid generating conflicts in code!
Here are the permalink settings that we use to generate the ultimate url structure:
my-shop
my-shop
(same as shop base)my-shop
/%product_cat%
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
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!
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!
Just perfect
Worked like a charm. Thanks!