How to disable WordPress canonical redirect to nearest matching URL and force 404 Page?

After doing some research, I have found the answer, albeit with a small limitation…. only shows the page with a trailing slash. This is perhaps technically correct url display so I’ll deal with it.

I found the solution buried in this similar Question
Disable WordPress URL auto complete

The CORRECT solution is by David Vielhuber – strangely is not voted as the correct solution. The upvoted solution perhaps worked for older WordPress versions, but no longer since WP v6.x.

To disable WordPress Canonical Redirect (to Nearest Matching URL) and force 404 Page… Add the following code to your child theme functions.php file.

// Disable WordPress canonical redirect to nearest matching URL and force 404 Page
// ---------------------------------
add_filter( 'redirect_canonical', function( $redirect_url ) {
    $url="http".((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']!=='off')?'s':'').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if( $redirect_url !== $url ) {
        global $wp_query;
        $wp_query->set_404();
        status_header( 404 );
        nocache_headers();
    }
    return false; 
});