In support of adding hook_page_not_found() to Drupal 7

I want to try and get a hook_page_not_found() to Drupal 7.

I've taken this snippet from

I have only just started this but I wanted to post for opinion. In IRC I was pushed towards using hook_page_alter() instead of defining a new hook. However hook_page_alter() runs after the 404 header has been sent. I don't expect the code as is to get in. Largely I am posting this as a start because I work on large sites ( and I don't want to have to use nfs to share the files directory as it adds another single point of failure in the stack.
Background for wanting hook_page_not_found()

The approach that Vignette and other large cm systems take is as follows.

Server X and Y are physical or logical Drupal servers that share the same codebase and database.

1) User A uploads an image ( sites/default/dog.png ) to server X to be included onto node/1234.
2) User B loads node/1234 on server Y. When the request comes into load sites/default/dog.png it fires a Drupal 404 page. If hook_404() exists a module of some sort could look up whether the file exists in the files table and go collect the image from the correct server, write the image locally and either serve the image or redirect to it. I am intentionally vague on how the module would collect the image from Server Y or Z or Q as this is an example use case.
2) User C loads node/1234 on server Y. When the request comes into load sites/default/dog.png the file exists and is served by the webserver. Apache never gets involved.

This approach doesn't need each upload to jump through hoops to put each file uploaded on each server which is fast for the person uploading. It doesn't require a cron job to push the relevant files around which would have time lag issues anyway.

Attached is a gz file, beautifully mangled by Drupal on upload and the two files in that mangled gz file that provide a sample module that could use hook_page_not_found()

hnf.tar_.gz446 bytes
hnf.info119 bytes
hnf.module264 bytes


Thanks moshe for the info.

Thanks moshe for the info. I'm going to try porting my module to the new API, and we'll see how well it works.

oops - it is in already

was looking at d6 by accident. d7 drupal_set_header() already uses drupal_static() so i think you can do this today without any patches. not sure if you are happy with this solution though.there is a small performance penalty for this. i usually throw a 404 in .htaccess for image files so that i don't touch drupal at all.


looks like that function got omitted at the last minute by

maybe no hook needed?

i hook might be a good idea. the thing is that our 404 handling is already swappable except for that 404 header. see the custom path feature in drupal_not_found().

one hackish approach is to convert drupal_not_found() to use drupal_static() instead of just static for $headers. then your custom 404 path could fiddle with the static and remove the 404 header and then proceed. this has the advantage of being a trivial patch to get committed.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.