00 Votes

Create Custom Error Pages for Website

Tip by Stefan Trost | Last update on 2022-12-27 | Created on 2011-12-23

If you do not care about your own error page for your website, usually an ugly webpage of the provider is displayed. This provider error page has the disadvantage that it has no relation to the actual website and the visitors probably are gone after viewing this page.

Much more beautiful, it is there to create a custom error page that is displayed, for example, if a page is not found. The advantages are obvious: The error page can be created in a design to match the rest of the home page and it can provide enough links, to prevent the migration of the user. You can even integrate an automatic search that offers suggestions based on the entered URL with which you can keep the user.

But how can we define own error pages for our website?

The .htaccess File

The route passes over the .htaccess file located in the root directory of the website. This file is called .htaccess (which is like only the extension) and the file may also include anything else you need. For the error documents we write the following lines into the .htaccess:

Errordocument 400 /errorpages/badrequest.html
Errordocument 401 /errorpages/unauthorized.html
Errordocument 403 /errorpages/forbidden.html
Errordocument 404 /errorpages/notfound.html
Errordocument 500 /errorpages/servererror.html

Each line stands for a specific error and it started with "ErrorDocument" and the respective error code such as 404 for Not Found (page not found). After that, the page is defined, which should be displayed as there are page. The pages can be named like you want and they can be saved anywhere, including on a completely different server. There will simply be a redirection to this page in the case of an error.

The Error Page

If you have mentioned the error documents in the .htaccess, you have to create just the appropriate error pages. Of course, this may also be PHP pages that are dynamically created fitting to the error and including information of what the user should now do best.

As mentioned above, it would be worth considering, for example, to start an automatic search to make the user other suggestions that may be of interest for him.

Since error pages should be excluded where possible, of course, you can also deposit a script on the error page that stores the URL. So, you can always see which pages have led to mistakes to improve them. In PHP, the URL we simply get with using $_SERVER["REQUEST_URI"]. Then, you can simply save the URL in a table or a file.

Otherwise, there are no rules for the error pages, only that they should be available. Because otherwise, we have again the problem, that the default error page will be displayed.


About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile


Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.