How to rewrite custom post type URL for multiple depths instead of one specific depth

I got this code that rewrite the url’s for custom posttypes. It works but only for a depth where the posttitle is on the 5th segment.
How can i adjust it so it accepts multiple depths?

e.g.:

http://www.mydomain.com/post_type/category1/postname/
http://www.mydomain.com/post_type/category1/child1/postname/
http://www.mydomain.com/post_type/category1/child1/child2/postname/
http://www.mydomain.com/post_type/category1/child1/child2/child3/postname/

i tried adding:

$newRules['product/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[3]';

But that did not work because it gave a 404 error while browsing the child3 depth in the example structure above.

Current code:

function mmp_rewrite_rules($rules) {
$newRules  = array();
$newRules['product/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[4]';
// The above structure only works if the postname is the 5th uri segment
// e.g. http://www.mydomain.com/post_type/category1/child1/child2/child3/postname/
$newRules['product/(.+)/?$']                = 'index.php?pcategory=$matches[1]'; 

return array_merge($newRules, $rules);
}

[UPDATE1]

It also won’t work if i change the code to if’s and elseif’s.
With elseif only the top rule is added and there fore i can only browse products where the posttitle is the 5th segment in the url.
With if all rules are added but i cannot browse the categories with a depth of 2,3 and 4 but u can browse products in depths where the url is in the 3,4 and 5th segment

function mmp_rewrite_rules($rules) {
$newRules  = array();
if ('index.php?products=$matches[4]'){
$newRules['product/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[4]';
} 
elseif ('index.php?products=$matches[3]'){
$newRules['product/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[3]';
}
elseif ('index.php?products=$matches[2]'){
$newRules['product/(.+)/(.+)/?$'] = 'index.php?products=$matches[2]';
}
// The above structure only works of the postname is the 5th uri segment
$newRules['product/(.+)/?$']                = 'index.php?pcategory=$matches[1]'; 

return array_merge($newRules, $rules);
}

Solutions Collecting From Web of "How to rewrite custom post type URL for multiple depths instead of one specific depth"

After testing countless different structures i came up with this. It’s manual work specifying each category but gives me more control.

If someone comes up with a better solution, please let me know.

function mmp_rewrite_rules($rules) {

//Product structure
$newRules['product/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[4]';
//The above structure only works of the postname is the 5th uri segment

//It's more flexible to confugure the depths for each category below
$newRules['product/televisies/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[3]';
$newRules['product/sports/(.+)/?$'] = 'index.php?products=$matches[1]';

//Category rule
$newRules['product/(.+)/?$']                = 'index.php?pcategory=$matches[1]'; 

return array_merge($newRules, $rules);
}

There’s a problem with your regex:

product/(.+)/(.+)/(.+)/(.+)/?$

The .+ part says to match anything as many times as possible. The anything can include /‘.

If you used something like

product/([^/]+/)+([^/]+)/?$

breaking this down, we have:

  • product/ which matches product/
  • ([^/]+/)+? – which matches as few as possible, but at least 1, slug/ elements (The [^/] part says match anything that isn’t a / )
  • ([^/]+)/?$ which matches a slug, possibly followed by a /, at the end of the line (which is what the $ means.

The post type will then be in $matches[2]