Site icon Hip-Hop Website Design and Development

WordPress shows front page, when it should show 404 with pagination style urls

Whenever I type a url for my site that contains three slashes "/" wordpress shows the front page instead of a 404.For example: example.com/something/does/notexist goes to frontpage, where example.com/something/doesnotexist/ correctly shows a 404.

I added several debug tools to see what’s happening. The rewrite rule explorer shows that no rule matched the request. Yet the front page is loaded. I assume this is related to pagination requests like /something/page/3. What I don’t understand is why wordpress does not actually do the query to see that nothing is there and therefore throw a 404? I already disabled all plugins and the theme’s functions.php. Still the same behaviour.
I find this issue sometimes on my life site but always on my local installation that runs valet. I can not see how this is a server issue though, as wordpress itself says "no rule matched" but then goes to the frontpage anyway.
I have searched for days now but can not find anything. Most related problems are the other way around, where a pagination link shows 404 when it shouldn’t.
Any help is appreciated.

PS: I did flush rewrite rules multiple times, nothing changed.

Edit:
I think I should make the url structure a little clearer:

example.com/a/b/c -> Goes to Frontpage (should do 404)
example.com/a/b/ -> Shows 404

It does not matter at all what I type in the url. As soon as there is three slashes with anything behind the last one, it always shows the frontpage.

Edit 2:
I tried running the site with wp server and it now correctly shows the 404 page for both urls. However, what is strange (and probably somehow related) is that for this type of url example.com/a/b/c wordpress makes an additional query after the get_page_by_path() returns no page found.

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'acf-disabled'
OR wp_posts.post_status = 'private')
ORDER BY wp_posts.post_date DESC
LIMIT 0, 11

This then becomes the Main Query. I can see the 404 page, but the posts property is populated, which has a lot of side effects for other hooks on the page.
It does not do this query when the url is just example.com/a/b/. Then wordpress just does to query the 404 page and that’s the end of it.