Yassin Rakha
Yassin Rakha

Reputation: 61

How to make a custom error page using .htaccess file

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

Upvotes: 1

Views: 1037

Answers (2)

MrWhite
MrWhite

Reputation: 45968

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:

Upvotes: 1

Calvin Bonner
Calvin Bonner

Reputation: 580

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.

Upvotes: 1

Related Questions