Adding this code in your theme’s 404.php file. Place this code at the very beginning:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>