Code Snippet

Home » Code Snippets » HTAccess » Custom Error Pages

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>

Subscribe to The Thread

  1. do the error pages have to be html?

    • Jonas

      No,
      it should be fine with any other file extention, too

  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

    What name to call the php template file?

    thanks.

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~