Two part question here.

What is the proper way in a module to determine if a page request is resulting in a HTTP 200 OK? Looking through static $stored_headers for a 30x/40x/50x seems wrong.

What is the earliest hook that you can reliably do this from?

1 Answer 1


When I was developing the code for Nodewords, I used the following code, to verify if Drupal was serving an error page:

  $headers = drupal_get_headers();

  if (preg_match('@HTTP/1\.[01]\x20+403@', $headers)) {
    return array('type' => NODEWORDS_TYPE_ERRORPAGE, 'id' => 403);

  if (preg_match('@HTTP/1\.[01]\x20+404@', $headers)) {
    return array('type' => NODEWORDS_TYPE_ERRORPAGE, 'id' => 404);

The function containing that code is called from nodewords_preprocess_page(), the implementation of hook_preprocess_page().

  • Thanks. This is essentially what I am doing. It just doesn't seem "right".
    – mpdonadio
    Aug 30, 2011 at 22:51
  • It's the only way I know of, as the path is not changed, when Drupal returns an error page. Suppose the user try to access example.com/404, when that page doesn't exist; Drupal shows the error page, but the URL the user would see is still the same.
    – apaderno
    Aug 30, 2011 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.