Redirect unexpected calls to custom error page using Apache2 web server

Introduction

Hi there! it is really important to handle unexpected HTTP response through web server if something went wrong with your application. Lets see how to show case your messages to the end users whenever something went wrong in your application.

Configuration update

Copy / Paste below configuration under virtualhost block in apache2 virtualhost.conf file and make sure that custom HTML pages which you have mentioned in the apache2 virtualhost.conf file exist under your document root. For example, here I have used /errorPage/HTTP404.html for 404 http response. So HTTP404.html file should be available under  document_root/errorPage location to display your customized 404 page.

1
2
3
4
5
6
7
8
ErrorDocument 400 /errorPage/HTTP400.html
ErrorDocument 401 /errorPage/HTTP401.html
ErrorDocument 403 /errorPage/HTTP403.html
ErrorDocument 404 /errorPage/HTTP404.html
ErrorDocument 500 /errorPage/HTTP500.html
ErrorDocument 501 /errorPage/HTTP501.html
ErrorDocument 502 /errorPage/HTTP502.html
ErrorDocument 503 /errorPage/HTTP503.html

Sample custom HTML pages

If you don’t have customized HTML pages to display in your website, then you can use this link to download sample customized HTML page to use it in your application / website.

Footer notes

If you have multiple domains hosted in the same server and you want to display customized http response page for each domains, then you should use different custom pages file under different document root.

Once you update apache virtual hosts’ conf file, then check the apache2 syntax and reload | restart the application to update the changes.

1
2
3
# apache2ctl configtest

# service apache2 reload|restart

That’s it 😉 Now you can show case customized messages to your end users whenever the application behaves abnormal. If you have any queries, feel free to ask in comment section 😀

Spread the word :)

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of