Custom Error Pages in .htaccess

This example shows you how to define custom error pages in .htaccess file and also how to display the error page on your site.

[View All Snippets]
Show Plain Text »
  1. # Sample 1: redirect errors to html files
  2. ErrorDocument 400 /400.html
  3. ErrorDocument 401 /401.html
  4. ErrorDocument 403 /403.html
  5. ErrorDocument 404 /404.html
  6. ErrorDocument 405 /405.html
  7. ErrorDocument 408 /408.html
  8. ErrorDocument 414 /414.html
  9. ErrorDocument 500 /500.html
  10. ErrorDocument 502 /502.html
  11. ErrorDocument 504 /504.html
  12.  
  13. # Sample 2: redirect errors to PHP file
  14. ErrorDocument 400 /error.php?q=400
  15. ErrorDocument 401 /error.php?q=401
  16. ErrorDocument 403 /error.php?q=403
  17. ErrorDocument 404 /error.php?q=404
  18. ErrorDocument 405 /error.php?q=405
  19. ErrorDocument 408 /error.php?q=408
  20. ErrorDocument 414 /error.php?q=414
  21. ErrorDocument 500 /error.php?q=500
  22. ErrorDocument 502 /error.php?q=502
  23. ErrorDocument 504 /error.php?q=504


   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:

  1. <?php
  2. // @file error.php
  3. $error_msg = array();
  4. if(isset($_GET["q"]) && is_numeric($_GET["q"])){
  5.    $status = array(
  6.       400 => array("400 Bad Request", "Your syntax is wack."),
  7.       401 => array("401 Login Error", "Please try again."),
  8.       403 => array("403 Forbidden", "This be private homey."),
  9.       404 => array("404 Missing", "Clearly this doesn't exist."),
  10.       405 => array("405 Method Not Allowed", "Method Not Allowed."),
  11.       408 => array("408 Request Timeout", "Upgrade your browser."),
  12.       414 => array("414 URL To Long", "Is that a URL in your pants?"),
  13.       500 => array("500 Internal Server Error", "AHHHhhh my server is down!"),
  14.       502 => array("502 Bad Gateway", "The server is acting all crazy."),
  15.       504 => array("504 Gateway Timeout", "I'm sorry, I'm afraid I can't do that."),
  16.    );
  17.    $error_msg = $status[$_GET["q"]];
  18. }
  19.  
  20. if(!empty($error_msg)){
  21.    foreach ($error_msg as $err) echo $err."<br>";
  22. }else{
  23.    echo "Something went wrong.";
  24. }
  25. ?>
Create Your Free Account
Please remember that this information is essential to use our services correctly.
After creating the account you will be able to download all of our FREE products.
Fields marked with * are mandatory






Please send me information about updates, new products, specials and discounts from ApPHP!
We recommend that your password should be at least 6 characters long and should be different from your username/email. Please use only letters of the English alphabet to enter your name.

Your e-mail address must be valid. We use e-mail for communication purposes (order notifications, etc). Therefore, it is essential to provide a valid e-mail address to be able to use our services correctly.

All your private data is confidential. We will never sell, exchange or market it in any way. Please refer to Privacy Policy.

By clicking "Create Account", you are indicating that you have read and agree to the ApPHP Terms & Conditions.