There are times when a specific page is not found. In such cases, we need to make sure that the 404.php appears before the user. This is a default function that WordPress does. WordPress looks for the 404.php page when it does not find the required page.
However, there are times when the server encounters such issues before WordPress notices. In such cases, you can still be sure that your server displays the 404.php template file by configuring your web server for custom 404 error handling.
In order to tell your web server to use the custom error files, you will have to edit the .htaccess file in the main directory (i.e. the place where your main index.php file resides) of your WordPress installation. If you do not have a .htaccess file or are not sure where to find it, you can refer to the article here.
To be sure that your server finds the 404 page, add the following line into your .htaccess file:
ErrorDocument 404 /index.php?error=404
The URL above /index.php is root-relative. This means that the forward slash begins with the root folder of your site. If WordPress is present in the subfolder or the subdirectory of your site’s root folder, named ‘wordpress’, the line you should add to your .htaccess file is:
ErrorDocument 404 /wordpress/index.php?error=404
WARNING: If you are using custom permalink, the trick above does not work, see this.
Wish to read a little more about this? Here is an article you can refer to.