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

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

3 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

3 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

3 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

3 years ago