Rewrite for page with a possible unknown parent page

You can use the pattern (([^/]*)/)? to match a full <component>/ construction – this will create a multi-dimensional element in the $matches array, with the first level containing the full expression and the internal matches containing an item with <component> without the “https://wordpress.stackexchange.com/” character. However, you need to be very careful in the way you handle the results, as it may show some unexpected behavior since you are basically trying to “merge” two components into one matched element – for example, if the entire element doesn’t exist (both components), all the subsequent matches will be moved with one position in the $matches array.

For more references on how the $matches array is created, study PHP’s preg_match_all method, as well as some good tutorials on regular expressions to figure out how those rewrite rules are built.

tech