If you are running a html website or your website is build on php and not using any CMS like wordpress or any framework like codeigniter then what you have to do is redirect your users when they visit a non existing web page on your website.
Let’s say your website do not have page http://www.yourwebstei.com/nopage.html and you want when user come to this url he redirects to index.php or any page you created for this error, let’s call that page 404.php which you have created in your root and can be visited like http://yoursite.com/404.php.
For redirection you can use JavaScript, and you can PHP as well , but they are not search engine friendly and search engine will able to visit that page which is not existing on your site. So to redirect it 301 and search engine friendly please create a new file with name and without any extension .htaccess and write the following code in that.
[php]
ErrorDocument 404 /404.php
[/php]
The above code will redirect your user from non existing page to 404.php page, and this is search engine friendly redirection. Remember browser call this 404 error page not found. And we can handle that this way.