Custom Error Pages with htaccess

Dec 18, 2014

Custom Error Pages with htaccess

Dec 18, 2014

HTML error pages

While browsing the Internet, we’d all have encountered errors at some point. The most common errors that show up are when we try to access a non-existent page or something failed during server processing. While this is undoubtedly a frustrating experience for any visitor, we can do our bit to get them out of the situation quickly. The solution is to have user-friendly error pages. Originally these errors would just state the reason along with the code – 404 page not found, 500 internal server error. Over time, many site owners realized that this does little in terms of helping the visitor navigate to a good page. Hence in nearly all cases, the visitor would simply move on to a different site. While the odd error is unavoidable, a custom error page can make all the difference in such a scenario. Instead of merely stating the error code, you can have a friendly message such as –

“Oops. You’re looking for something that isn’t here. You can use our search box for the look it up or navigate to our homepage.”

All standard WordPress themes include error pages as part of the package. These pages include the header, footer, sidebar, etc and hence look very aesthetic. However, you may want to add your own function to these error pages. For instance, you may want your 404 page to send you an email whenever a visitor ends up there, along with the broken URL. This makes it very easy for you to fix any broken links on your site.

How do you add your own custom error pages to your site and server them to your visitors? Using htaccess, of course! Once you create your error pages, add the following rules to the htaccess file –

ErrorDocument 404 /error404.htm

What the above directive means is that whenever a 404 error is encountered, the error404.htm page is loaded. You can include different error pages corresponding to the error type. Some of the common ones are –

400 – Bad request
401 – Authorization required
403 – Forbidden directory
404 – Page not found
500 – Internal Server Error

For each error page that you want to customize, simply create the page and add the corresponding rule to your htaccess-

ErrorDocument 401 /error401.htm
ErrorDocument 403 /error403.htm
ErrorDocument 500 /error500.htm

Would love your thoughts, please comment.x