Define custom Error pages using htaccess

Having a 404, 403 or 500 Internal Server Error is not a new thing for any web application. Every time a web app encounters any client or server side issue the browser automatically renders the default error content stating the message. But if you want your application to show custom content than you can do it by defining some rules in htaccess file of your sever.

All you have to do is log in to your server. Open your htaccess file and paste these lines. These rules are for Apache server, for other servers like IIS there are other methods to define the custom error pages. I’ll cover them in other post.

ErrorDocument 404 /404.php
ErrorDocument 403 /403.php

Now in these two lines what you just did is created two rules for the errors. One for 404 and one for 403.

The first word ‘ErrorDocument’ indicates the type of rule you are applying. Second word indicates the type of error you are specifying and the third one is the path for page that you want to show when that particular error occurs.

This rule is not just for 403 or 404 error you can define any error and redirect the user to that particular page.

Take a look at Google’s custom 404 page –

Why you should do that –

It increases the UX of your page and it makes your application search engine friendly.