Check if the given URL is leading to a 404 page

Check if the given URL is leading to a 404 page

March 28, 2013

With all the features, CMSs are giving us today, one of the cutest is certainly a custom 404 page. Just look what are designers doing with it. It’s almost art 🙂

However, I also made a custom 404 page for one of my project, and I wanted to test if the server is also returning the 404 header, along all the beauty of this custom 404 page. In this purpose, I have written the following code:

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);

/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);

/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
  /* Handle 404 here. */
  echo "404";


/* Handle $response here. */

With this code you don’t need to check the the header manually in the console. Just run the script and the answer to your question will be displayed (An “404” string if the page returned a 404 header).


Follow deanpodgornik on

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *