Handling error 404 "File not found"


Error 404 (File not found) occurs when a user requests a document from the web server that is not on disk. That is, when a user requests a non-existent file. This is the most "popular" of all errors. You can observe the occurrence of such errors in the error_log file and take this into account — track which documents are "not found" on the server and correct incorrect links from your html documents. Also, links to non-existent documents may lead from other sites.


For the convenience of site visitors, you can handle 404 errors by replacing the standard web server responses with your own pages with the necessary description of the cause of the error and tips on what to do in this case. It is also useful to place a map of your site or a link to a search engine on such a page.


How to handle the 404 error. Create a www file in the directory.htaccess and put the following instruction for the Apache web server in it:


ErrorDocument 404 /not-found.html

Then create a file not-found.html in which you put a description of the error that occurred and tips on what to do in this case to the visitor of your site. Now a user who types the address of a non-existent page in his browser will be automatically redirected by the server to the file not-found.html .


Using the ErrorDocument directive, you can handle not only the 404 error, but also almost all other errors. For example, you can handle error 403 in the same way.


Attention: the peculiarity of MS Internet Explorer browsers versions 5.xx and 6.xx is that if your error file is less than 512 bytes in size, the browser will show the user not your file, but its own 404 error message. The simplest solution in this situation is to fill the error file with spaces so that its size exceeds 512 bytes.