Help With Rewrite_Rules For a Custom Plug-In

In regards to when you need to be flushing the rewrite rules:

This should only happen once, make sure you are not calling flush_rules() on init, as this will really kill performance for high traffic sites.

WordPress stores all of the rewrites in an array in the database, so all you need to do is hook in just before it updates the database. One way to do it is hook into “rewrite_rules_array” and add your rule:

add_filter( 'rewrite_rules_array', 'my_rewrites_function' );

function my_rewrite_function( $rules ) {

    $my_rules = array(
        '([^/]+)/([^/]+)/([^/]+)/?' => 'index.php?pagename=$matches[1]&prodid=$matches[2]&pname=$matches[3]'
    );
    $rules = array_merge( $my_rules, $rules );

    return $rules;
}

Keep your ‘query_vars’ hook as it is. You will need to visit the Permalinks page to insert your rule into WordPress’ options – but then you should be set.

If it doesn’t work, you can hook into ‘parse_request’ to see which rewrite rule was chosen for the page you are viewing:

add_filter( 'parse_request', 'my_request_rewrite_check' );

function my_request_check( $wp ) {

    var_dump( $wp->matched_rule );
    return $wp;
}

Also, your regex is a but loose for the structure you put above mywebsite.com/products/food-and-catering-workwear/232/Trilby Hat/, it would be more like:

^products/([^/]+)/([\d]*)/([^/]+)/?$

Is says “starts with ‘products’, “https://wordpress.stackexchange.com/”, then any word, “https://wordpress.stackexchange.com/”, then any numbers, “https://wordpress.stackexchange.com/”, then any word” (“$” means that the string has to now end)

I hope that helps!