HTTP 404 Not Found

Overview

The HTTP 404 Not Found status code indicates that the server cannot find the requested resource. This response code is one of the most recognizable and is often used when the server cannot find a matching resource for the client’s request.

Purpose

The HTTP 404 response is used to signify that the server has not found anything matching the requested URI (Uniform Resource Identifier). It does not specify whether the condition is temporary or permanent.

Usage

Client Behavior:

  1. Send Request: The client sends an HTTP request to access a specific resource.
  2. Receive Response: The client receives the HTTP 404 status code, indicating that the requested resource cannot be found on the server.

Server Behavior:

  1. Search for Resource: The server attempts to locate the resource specified in the request.
  2. Send Response: If the resource cannot be found, the server sends a 404 Not Found response.

Scenarios

  • Invalid URLs: Accessing a URL that does not correspond to any resource on the server.
  • Resource Removal: Attempting to access a resource that has been removed or relocated.

Sequence Diagram

Illustrating the process for an HTTP 404 response:

sequenceDiagram
    participant Client
    participant Server as Web Server

    Note over Client: Client requests a non-existent resource
    Client->>Server: GET /non-existent-page HTTP/1.1
    Note over Server: Server cannot find the resource
    Server->>Client: HTTP/1.1 404 Not Found

Curl Request and Response Example

Sending a request using Curl to a non-existent resource:

curl -i http://example.com/non-existent-page
# Expected response: HTTP/1.1 404 Not Found

PHP cURL Request and Response Example

PHP script using cURL to handle a 404 Not Found response:

<?php
$ch = curl_init('http://example.com/non-existent-page');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 404) {
    echo "The requested resource was not found.";
}
curl_close($ch);
?>

Python Request and Response Example

Python script to send a GET request and handle a 404 Not Found response:

import requests
response = requests.get('http://example.com/non-existent-page')
if response.status_code == 404:
    print("The requested resource was not found")

Apache Configuration for HTTP 404 Not Found

Configuring Apache to handle 404 errors:

<VirtualHost *:80>
    ServerName example.com
    # Custom error document for 404 Not Found
    ErrorDocument 404 /custom-404.html
</VirtualHost>

NGINX Configuration for HTTP 404 Not Found

Setting up NGINX to respond to 404 errors:

server {
    listen 80;
    server_name example.com;
    location / {
        try_files $uri $uri/ =404;
    }
    # Optional custom error page
    error_page 404 /custom-404.html;
}

HTTP 403 Forbidden HTTP 405 Method Not Allowed


 

Free Weekly

Newsletter

Join my weekly newsletter for the latest in tech! You'll get neat coding tricks, trend updates, career advice, SaaS reviews, crypto, bitcoin, and financial tips. All straight to your inbox, designed to keep you ahead.