This guide walks you through creating and serving your own 404.php page using .htaccess with Apache on local servers like WAMP, XAMPP, or live hosting environments.
- Apache server (WAMP, XAMPP, or live Linux host)
- PHP installed and running
.htaccessenabled in your Apache config (AllowOverride All)- A working virtual host or localhost project directory
/www/myprojectInside it, create:
myproject/
├── index.php
├── 404.php
└── .htaccessCreate a basic index file to test the project is running.
<!-- index.php -->
<!DOCTYPE html>
<html>
<head><title>Home</title></head>
<body>
<h1>Welcome to My Project</h1>
</body>
</html><!-- 404.php -->
<!DOCTYPE html>
<html>
<head><title>404 Not Found</title></head>
<body>
<h1>Oops! Page Not Found (404)</h1>
<p>The page you're looking for doesn't exist.</p>
<a href="/">Go back home</a>
</body>
</html># .htaccess
# Set custom 404 error page
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
RewriteEngine On
# Prevent directory listing
Options -Indexes
# Protect .htaccess from exposure
<Files .htaccess>
Order allow,deny
Deny from all
</Files>
</IfModule>- For WAMP:
C:\wamp64\bin\apache\apache2.x.x\conf\httpd.conf - For XAMPP:
C:\xampp\apache\conf\httpd.conf
Uncomment this line:
# LoadModule rewrite_module modules/mod_rewrite.so➡️ Change to:
LoadModule rewrite_module modules/mod_rewrite.soThen locate:
<Directory "c:/wamp64/www/">
AllowOverride None➡️ Change to:
<Directory "c:/wamp64/www/">
AllowOverride AllRestart Apache after saving changes.
- Visit:
http://localhost/myproject/→ Should loadindex.php - Visit:
http://localhost/myproject/nonexistentpage→ Should load your404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]In php.ini, ensure:
display_errors = On
log_errors = On
error_log = "C:\path\to\php-error.log"myproject/
├── .htaccess
├── 404.php
└── index.php