Home > Software design >  How to make a custom error page using .htaccess file
How to make a custom error page using .htaccess file

Time:10-01

I am trying to raise a custom error page for each error

Here is my directory

.htaccess
error_pages:
           error_400.html
           error_401.html
           error_404.html

CodePudding user response:

You are close. First, if you have not done so already, you'll need to initialize your .htaccess document.

RewriteEngine On

Next, you'll need to define the error and tell .htaccess which file to use for that error:

ErrorDocument 400 /error_pages/error_400.html
ErrorDocument 401 /error_pages/error_401.html
ErrorDocument 404 /error_pages/error_404.html

Hopefully this helps.

CodePudding user response:

You've tagged your question php, but used .html in your example? If you are using PHP then you don't need separate error documents for each HTTP status. You can use the same PHP error document and check the REDIRECT_STATUS (ie. $_SERVER['REDIRECT_STATUS'] superglobal) in order to determine the status of the error that triggered the error document.

So, your .htaccess file would consist of:

ErrorDocument 400 /error_pages/error_handler.php
ErrorDocument 401 /error_pages/error_handler.php
ErrorDocument 404 /error_pages/error_handler.php

Reference:

  • Related