url rewriting - How to set product category base the same as shop base in WooCommerce?

admin2025-01-07  4

I'm using WooCommerce on a WordPress. I would like my products and product categories to have the same url base. E.g.

www.example/shop/product-category
www.example/shop/sample-product

Using this code here:

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

I changed the permalinks in Settings > permalink:

Custom Structure: /shop/%postname%/
Product category base: shop
Custom base: shop

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

Custom Structure: works
Product category base: works
Custom base: 404 error

Anybody have any insight on how to get the single product pages to not throw the 404 error?

Thanks!

I'm using WooCommerce on a WordPress. I would like my products and product categories to have the same url base. E.g.

www.example.com/shop/product-category
www.example.com/shop/sample-product

Using this code here:

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

I changed the permalinks in Settings > permalink:

Custom Structure: /shop/%postname%/
Product category base: shop
Custom base: shop

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

Custom Structure: works
Product category base: works
Custom base: 404 error

Anybody have any insight on how to get the single product pages to not throw the 404 error?

Thanks!

Share Improve this question edited May 21, 2023 at 14:34 fuxia 107k38 gold badges255 silver badges459 bronze badges asked May 21, 2023 at 10:01 PawełPaweł 1 1
  • Help me, please! – Paweł Commented May 23, 2023 at 7:56
Add a comment  | 

2 Answers 2

Reset to default 0

Lots of people dealing with the same issue and I haven't found a solution yet either. It's like the whole thing is broken and most people just don't care?

It worked for me by also adding this:

add_action( 'init', function() {
  flush_rewrite_rules();
} );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256784a389.html

最新回复(0)