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.