Return to Snippet

Revision: 64523
at August 18, 2013 19:32 by apphp-snippets


Initial Code
# Sample 1: redirect errors to html files
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
 
# Sample 2: redirect errors to PHP file
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

<?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", "Please try again."),
          403 => array("403 Forbidden", "This be private homey."),
          404 => array("404 Missing", "Clearly this doesn't exist."),
          405 => array("405 Method Not Allowed", "Method Not Allowed."),
          408 => array("408 Request Timeout", "Upgrade your 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, 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.";
    }
?>

Initial URL
http://www.apphp.com/index.php?snippet=htaccess-custom-error-pages

Initial Description
This example shows you how to define custom error pages in .htaccess file and also how to display the error page on your site.  You may create separate files for each error, described in Sample 1 or just create the one PHP file for Sample 2.    Here the PHP template for dealing with any sort of error, if you want to keep it simple:

Initial Title
Custom Error Pages in .htaccess

Initial Tags
error

Initial Language
PHP