Sling Academy
Home/DevOps/Apache: How to Display a Custom 404 Not Found Page

Apache: How to Display a Custom 404 Not Found Page

Last updated: January 20, 2024

Introduction

Encountering a 404 Not Found error can be a frustrating experience for website visitors. However, presenting a custom 404 page can turn a potentially negative experience into a more positive one, by providing users with helpful information and maintaining your site’s branding. Apache, the widely-used web server software, offers flexible options for customizing error pages. This tutorial will guide you through the process of configuring Apache to display a custom 404 Not Found page, enhancing your user experience.

Prerequisites

  • Access to an Apache web server
  • Basic knowledge of server configuration
  • A text editor
  • An existing website or application running on Apache

Step-by-Step Guide

Step 1: Create a Custom 404 Page

Before configuring Apache, you’ll need to create an HTML document that will serve as your custom 404 page. Craft this page to reflect your website’s design, include navigation links, and potentially offer a search box or link back to the homepage. Here’s a simple example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Page Not Found</title>
</head>
<body>
    <h1>Oops! Page not found.</h1>
    <p>We can't seem to find the page you're looking for.</p>
    <p><a href="/">Return to the Sling Academy's home page</a></p>
</body>
</html>

Save this file with a .html extension, for example, ‘404.html’, in a publicly accessible directory on your server.

Step 2: Configuring Apache to Use the Custom 404 Page

To tell Apache to use the custom 404 page, you need to edit your Apache configuration file or a .htaccess file if you’re allowed to use them on your server. The simplest method is to modify the .htaccess file which is typically located in the root of your website’s directory.

Add the following line to the .htaccess file:

ErrorDocument 404 /404.html

This directive tells Apache to serve your custom ‘404.html’ page whenever a 404 error occurs. Replace ‘/404.html’ with the actual path to your custom page if it’s different.

If you prefer or need to edit the virtual host file (typically used for server-wide configuration), you’ll need to find the corresponding <VirtualHost> block for your website and add the ErrorDocument directive there. Here’s an example:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html
    ServerName yourdomain.com
    ErrorDocument 404 /errors/404.html

    <Directory /var/www/html>
        AllowOverride All
    </Directory>
</VirtualHost>

Note that in this example, the custom 404 page is stored in an ‘errors’ subdirectory.

Step 3: Verify the Configuration

After making changes to .htaccess or the Apache configuration file, it’s always a good idea to check your changes. You can do this by navigating to a non-existent URL on your website. You should see your custom 404 page displayed.

Step 4: Advanced Customization

Apache also allows for even more advanced customization of error documents. For example, you can serve different error documents for different content or script a dynamic error page that logs errors or analyzes the request before presenting a response to the user. Here’s how you might script a PHP page to handle errors:

ErrorDocument 404 /error-script.php

And your ‘error-script.php’ could look like :

<?php
header("HTTP/1.1 404 Not Found");
include("header.php"); // Common header

$log_file = "/var/log/apache2/error_log";
$current = file_get_contents($log_file);
$current .= "404 error at " . date('Y-m-d H:i:s') . " for " . $_SERVER['REQUEST_URI'] . "\n";
file_put_contents($log_file, $current);

// Your HTML error content here...

include("footer.php"); // Common footer
?>

This PHP script logs the 404 error and then includes common header and footer files for your site, allowing you to maintain a consistent look throughout the user interaction.

Step 5: Ensure Good Practice

When creating custom error pages, it’s important to ensure that they return the correct status code. The page itself should respond with a 404 HTTP status code, not a 200 OK. This is crucial for SEO and user experience as well as for web service interactions which may depend on correct error codes being returned.

Conclusion

By designing and implementing a custom 404 not found page, you significantly enhance user experience and maintain the professional appearance of your site during the inevitable occurrence of broken or missing links. With this tutorial, you’ve learned how to configure Apache to use a custom 404 page, keeping your users engaged and informed in the event of an error.

Next Article: Apache: How to Show a Maintenance Page on Demand

Previous Article: How to Enable CGI Scripts in Apache on Ubuntu

Series: Apache Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide