Error handling on a local Windows-based, Apache, server
This time, we'll be setting up very basic error handling, and setup our first .htaccess file.
If you've been following along, we've got no error handling in place. So, if you try to access a page that doesn't exist, like http://website.localhost/does_not_exist.html, you'll get a basic error page that comes straight out of Apache.
The problem with this is at least two-fold. First, someone will be able to determine our site's server software if they just try to hit a page that doesn't exist. Second, if we created an actual Web site, we'd lose visitors. What we want to do is give the visitor an explanation of what may have happened, and offer them solutions – alternative pages and the like.
This time, we'll only be setting up a very basic, HTML, page. In a future article, we'll discuss how to create a more robust error handling page.
First, we'll want to create the page that the user will see. While we're doing this, we'll want to keep in mind that we should keep the file size relatively small, but larger than 512 bytes (due to Internet Explorer not being able to handle error pages smaller than this).
Something like the following should be enough.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<br /><br />
<p>I'm sorry, but the page that you're attempting to access either never existed, or no longer exists.</p>
<p>Please visit our <a href="/">home page</a> to continue.</p>
<p>If you came upon this error by clicking on a link on this site, please contact the <a href="mailto:[email protected]_domain">Webmaster</a>.</p>
Copy the above into a new file, and save it as error404.html in your website folder (c:\home\website\public_html\ if you've been following along).
Now, create a new file in Notepad and paste the following into it:
ErrorDocument 404 /error404.html
Now, save this file as ".htaccess", again into your website folder (c:\home\website\public_html\). Note that there is a period before htaccess, and there is no extension.
Now, we'll make a slight modification to the Apache httpd.conf file ("C:\Program Files\Apache Group\Apache\conf\httpd.conf" if you've been following along). Open this file and do a search for htaccess. The first uncommented line after your first find should be the following:
Change this to:
Now save httpd.conf, close the file, and restart Apache using the Services control panel.
Now, visit a page on "website" that doesn't exist, like we did before - http://website.localhost/does_not_exist.html
Once you've done this, you should notice that the standard Apache error has been replaced by your custom error page. You can go ahead and custom this additionally, as necessary.
Support This Site
If my blog was helpful to you, then please consider visiting my Amazon Wishlist.