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:

  • Gabriel

    Hello,

    Thanks for this. First when I tried it seemed to work but for some reason it fails when I create new categories. Any thoughts?

    Cheers

  • Gabriel

    I also get strange redirects from these sub-categories on the left, to these products on the right:

    /produkter/kontaktdon/cirkulara/ ==> /produkter/bakkapor/cirkulara-2/
    /produkter/kontaktdon/rektangulara/ ==> /produkter/bakkapor/rektangulara-2/

    I guess this is what people warns about when trying to accomplish this.

    In other words, I can’t list the products of the sub-categories /cirkulara/ and /rektangulara/.

    G.

  • Can

    I’m getting error in “code snippets plugin” syntax error, unexpected T_VARIABLE

  • Amel

    This did not work for me. I added the code to the bottom of my themes functions.php file, changed the product-category page to “products”, and went into my pages and changed the page name and slug to products. I then went and updated my permalinks so that it shows http://www.domain.com/products/%product_cat%/%products% and no luck.

    Am I missing a step?

  • Derek

    Wowsers! You have come up with a very handy solution here Mark! High fives all the way around and thank you for posting this solution. And I quote ‘It Just Works.’