Custom Error Pages

A generic server error message can confuse visitors and leak information about your stack. Custom error pages let you maintain branding and provide helpful guidance when something goes wrong.

Configuring Apache

Use the ErrorDocument directive in your .htaccess or virtual host config to point to your custom pages:

ErrorDocument 404 /error/404.php ErrorDocument 500 /error/500.php

Ensure that the error directory isn't publicly listed and that your error scripts send the correct HTTP status codes.

Creating the pages

A simple 404 page might look like this:

<?php http_response_code(404); ?><!DOCTYPE html> <html lang="en"> <head> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <meta charset="utf-8"/> <title>Page Not Found</title> <link rel="stylesheet" href="/skins/default/css/perf.css"> </head> <body> <nav class="sidebar-nav drawer-nav"> <a href="/" class="nav-item active"><span class="material-icons-outlined">chair</span> Homepage</a> <a href="/php-coding-services" class="nav-item"><span class="material-icons-outlined">handyman</span> My Services</a> <a href="/premium-php-scripts" class="nav-item"><span class="material-icons-outlined">inventory_2</span> PHP Scripts</a> <a href="/html-admin-templates" class="nav-item"><span class="material-icons-outlined">diamond</span> HTML Templates</a> <a href="/personal-portfolio" class="nav-item"><span class="material-icons-outlined">person</span> My Portfolio</a> <a href="/lamp-tutorials" class="nav-item"><span class="material-icons-outlined">menu_book</span> Tutorials</a> </nav> <h1>404 Not Found</h1> <p>Sorry, the page you requested could not be found.</p> <a href="/">Return home</a> <footer class="app-footer"> <div style="font-family: 'Times New Roman', Times, serif; text-transform: uppercase; letter-spacing: 2px; line-height: 1.6; max-width: 700px; margin: 0 auto 1.5rem auto; color: #666; font-size: 0.85rem; border-bottom: 1px solid #333; padding-bottom: 1.5rem;"> "I hereby command you: Be strong and courageous; do not be frightened or dismayed, for the Lord your God is with you wherever you go." <br> <span style="font-weight: 700; display: block; margin-top: 0.5rem; color: #888;">Joshua 1:9</span> </div> <p>© 2025 <a href="/" title="PHPOG.COM" class="footer-link">PHPOG.COM</a>. All Rights Reserved.</p> </footer> </body> </html>

Always set the appropriate response code using http_response_code(). Do not redirect errors to a successful page, as search engines will not see the error.

Always set the appropriate response code using http_response_code(). Do not redirect errors to a successful page, as search engines will not see the error.