Replace ErrorDocument with RewriteRule (.*) /handler.php [L]
In your handler.php, you can then capture the REQUEST URI, POST data whatevere and then based on your logic, you can decide whether to return a HTTP/1.1 404 Page not found error or return normal response HTTP/1.1 200