Rewrite product and shop base the same

By default, Woocommerce uses a different base permalink structure for product and shop. But suppose you want to be able to use the following URLs

Shop: /products

Shop category: /products/product-category/

Product: /products/product-category/product-name

…then add the following permalinks programmatically to your functions.php

add_filter( 'rewrite_rules_array', function( $rules ) {
    $new_rules = [
        'products/([^/]*?)/?$' => 'index.php?product_cat=$matches[1]',
        'products/([^/]*?)/([^/]*?)?$' => 'index.php?product_cat=$matches[1]&product=$matches[2]',
        'products/([^/]*?)/page/([0-9]{1,})/?$' => 'index.php?product_cat=$matches[1]&paged=$matches[2]',
    return $new_rules + $rules;

I'm Robbert Vermeulen. I document everything I learn and help thousands of people with coding WordPress. My site has no ads or sponsors. If you enjoy my content, please consider supporting what I do.


    No comments yet..

Leave a reply

Feel free to ask questions or make comments.