How does WordPress manage to differentiate between post and page URLs without a distinct base, and how can I replicate this functionality?

I dont know how I missed out on that while looking for aproaches, but I have a working solution now based on this answer.

I modified it to check for slashes in $query->request, if there is none I first look for my custom post type (get_posts() with'slug' => $query->request). If no post is found I do the same for the taxonomy (get_terms()).
If there is one slash, I explode the $query->request to $parts and look for a custom post with 'slug' => $parts[1] and a tax_query array for the custom taxonomy.
Whenever something is found I do something like:

$new_query = array(
    'post_type' => 'my_post_type',
    'name' => $name
);
$query->query_vars = $new_query;

If nothing is found the query is untouched, and in the end I just return $query;

deneme bonusu veren sitelerbahis casinomakrobetceltabettipobet365pinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş