Grow your CSS skills. Land your dream job.

Last updated on:

Custom Error Pages

ErrorDocument 400 /400.html
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 405 /405.html
ErrorDocument 408 /408.html
ErrorDocument 414 /414.html
ErrorDocument 500 /500.html
ErrorDocument 502 /502.html
ErrorDocument 504 /504.html

And a PHP template for dealing with any sort of error, if you want to keep it simple:

<?php

$status=$_SERVER['REDIRECT_STATUS'];
$codes=array(
      400 => array('400 Bad Request', 'The request cannot be fulfilled due to bad syntax.'),
      401 => array('401 Login Error', 'It appears that the password and/or user-name you entered was incorrect.'),
      403 => array('403 Forbidden', 'Sorry, employees and staff only.'),
      404 => array('404 Missing', 'We\'re sorry, but the page you\'re looking for is missing, hiding, or maybe it moved somewhere else and forgot to tell you.'),
      405 => array('405 Method Not Allowed', 'The method specified in the Request-Line is not allowed for the specified resource.'),
      408 => array('408 Request Timeout', 'Your browser failed to send a request in the time allowed by the server.'),
      414 => array('414 URL To Long', 'The URL you entered is longer than the maximum length.'),
      500 => array('500 Internal Server Error', 'The request was unsuccessful due to an unexpected condition encountered by the server.'),
      502 => array('502 Bad Gateway', 'The server received an invalid response from the upstream server while trying to fulfill the request.'),
      504 => array('504 Gateway Timeout', 'The upstream server failed to send a request in the time allowed by the server.'),
);

$errortitle=$codes[$status][0];
$message=$codes[$status][1];

if($errortitle==false){
       $errortitle="Unknown Error";
       $message="An unknown error has occurred.";
}

?>
<!doctype html>
<html>
<head>
<title><?php echo("$errortitle");?></title>
<meta charset="utf-8">
</head>
<body>

<!-- Insert headers here. -->

<?php
echo('<h1>'.$errortitle.'</h1>');
echo('<p>'.$message.'</p>');
?>

<!-- Insert footers here. -->

</body>
</html>

Comments

  1. do the error pages have to be html?

  2. Hey, I used this snippet for .htaccess and also made the error pages but for some reason, its not working. I can’t see my custom error pages even though I’ve checked all file paths and everything.
    where did I go wrong?
    my htaccess file looks like this:

    ErrorDocument 400 /errors/badrequest.html
    ErrorDocument 401 /errors/authreqd.html
    ErrorDocument 403 /errors/forbid.html
    ErrorDocument 404 /errors/notfound.html
    ErrorDocument 500 /errors/serverr.html
    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^http://(.+\.)?qumatech\.com/ [NC]
    RewriteCond %{HTTP_REFERER} !^$
    RewriteRule .*\.(jpg|gif|bmp|png)$ /images/dontsteal.gif [L]

    please help, I am new with htaccessing :/
    thank you!

  3. This was a great addition to my code.

  4. Neil
    Permalink to comment#

    What name to call the php template file?

    thanks.

  5. Permalink to comment#

    This worked for me:
    # .htaccesss
    # redirect errors
    ErrorDocument 400 /error.php?q=400
    ErrorDocument 401 /error.php?q=401
    ErrorDocument 403 /error.php?q=403
    ErrorDocument 404 /error.php?q=404
    ErrorDocument 405 /error.php?q=405
    ErrorDocument 408 /error.php?q=408
    ErrorDocument 414 /error.php?q=414
    ErrorDocument 500 /error.php?q=500
    ErrorDocument 502 /error.php?q=502
    ErrorDocument 504 /error.php?q=504

    
    &lt?php
    // @file error.php
      $error_msg = array();
      if (isset($_GET['q']) && is_numeric($_GET['q'])) {
        $status = array(
               400 => array('400 Bad Request', 'Your syntax is wack.'),
          401 => array('401 Login Error', 'Try again sucka.'),
          403 => array('403 Forbidden', 'This be private homey.'),
          404 => array('404 Missing', "Clearly this doesn't exist."),
          405 => array('405 Method Not Allowed', 'This resource is too good fo yo method.'),
          408 => array('408 Request Timeout', "Upgrade your effin' browser."),
          414 => array('414 URL To Long', "Is that a URL in your pants?"),
          500 => array('500 Internal Server Error', 'AHHHhhh my server is down!'),
          502 => array('502 Bad Gateway', 'The server is acting all crazy.'),
          504 => array('504 Gateway Timeout', "I'm sorry Dave, I'm afraid I can't do that."),
        );
        $error_msg = $status[$_GET['q']];
      }
      
      if (!empty($error_msg)) {
        foreach ($error_msg as $err) {
          echo $err . '<br>';
        }
      }
      else {
        echo 'Something went wrong.';
      }
    ?>
    
    • Permalink to comment#

      I’ve been trying to use this version, as well as the original posted at the top, but neither of them are working. I get the default 404 error, but then I also get this:

      Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

      Wondering if you can shed some light on this? Maybe I’m just not implementing this correctly? I’ve googled this and looked for alternatives, but I can’t find anything as simple as this.

      And yes, before you recommend, I did fix the error at the very top of your version, where it should read <?php, rather than &lt?php.

    • shaq
      Permalink to comment#

      Hi Andy

      i use your code 2 weeks ago and foud:

      if you put “[[%&” in your url like

      http://example.com/%&

      then comes the error page from apache and not from the in .htaccess generated
      custom “ErrorDocument 400 /error.php?q=400″ error.php-page with the info:

      400 Bad Request’, ‘Your syntax is wack.

      how could i fix this ?

  6. Permalink to comment#

    Hi
    Your code is missing a semi-colon at the end of line 22 :)

  7. Alan
    Permalink to comment#

    This worked perfect on my site. This was exactly what I was looking for because I did not want to Maintain several Error Pages. Now My error pages look like my main pages.

    Thanks for a useful code

  8. Worked and nice. with little addition on javascript and CSS3 i was able to make it stylish and multi language

    check this out : http://salim.web.id/error.php

  9. Shaq
    Permalink to comment#

    I will reply to myself:

    read this about apache and nginx

    Hi Andy
    i use your code 2 weeks ago and foud:
    if you put “[[%&” in your url like

    http://example.com/%&…….

    thanks anyway

  10. Shaq
    Permalink to comment#

    I hope my last question about ErrorDocuments

    :-)

    whats wrong with this RewriteRule above?

    why are a blocked files/ursl not redirected to a generated 404 site, instead i see the “if($errortitle==false){…” method???

    RewriteRule .* error.php?q=404 [NS,L]

    thanks for help

  11. Permalink to comment#

    Finally code that works! ^^ Thank you very much.

  12. adarsh
    Permalink to comment#

    where to place them in wordpress?
    what directory and extension ?

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
    ``` 
    <div>
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
    </div>
    ```
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".