Categories: Development

Stating a 404 response with PHP; Why it is important

HTTP 404 is the response given when you browsers interacted with the servers but no page was found at the location (URL).

This is important to know because for SEO and Google crawlers if they find an error such as nothing exists on the page or there is missing data a 404 will tell them that in theory nothing exists at this location, do not index.

404 saves the day

I ran into the problem with vanity URL’s fetching and displaying data on the page based of a GET request in the URL. Meaning if there was no row in the database for that id the page would load up the predefined HTML without the dynamic PHP displayed values. It was unreadable and not a complete page.

Admitting the page doesn’t exist is the right way

This is an issue because theoretically infinite pages could exists that are broken and make no sense. I fixed this by checking for no rows found then display the 404 page not found error thus preventing bad crawl and SEO results.

This is simple done by setting the header:

header("HTTP/1.0 404 Not Found");

This will then state a 404 response and show the default 404 page (if any).

To state 404 and redirect to custom 404 page:

http_response_code(404);
include('custom_404.php');
die();

Very simple and actually underrated as being crucial in being a web crawler friendly website.

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago