How to check if the current page is a 404 error from a module/template?
In Drupal 7, you can use drupal_get_http_header().

In the template.php file, use this code.

$status = drupal_get_http_header("status");
if ($status === '404 Not Found'){
  // Do something.
}


In Drupal 8, you can use the following code in a hook.

$route_name = \Drupal::request()-&gt;attributes-&gt;get('_route');
if ('system.404' === $route_name) {
  // Do something.
}