'How to disable default url structure (page_id=id) in Wordpress and just use permalink structure

I would like to stop WordPress from redirecting the default URL pattern example.com/?page_id=id to the permalink. I would like it to result in a 404.

I don't want that users are able to find single pages by brute forcing my URL structure in a way that they are just trying out URLs with various page_ids.

My goal is to be able to use "hidden" pages with a random slug, so that only people who know the URL will be able to find the corresponding page. But if the pages are accessible via the page id that wouldn't make much sense.

I assume that there is a way to do that in WordPress itself. I don't want to use web server redirects like with the htaccess file, because I want it to be coupled more tightly with the wordpress application. In my experience htaccess files are sometimes messed up by inexperienced users. I want to prevent that the page_id redirections are accidently re-enabled if the htaccess gets messed up unnoticed.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source