One of the things that a default Drupal installation does is that it handles 404s for static files within Drupal itself. In other words, a 404 for a .css or .jpg file causes a full Drupal bootstrap. This is not necessary, and wastes resources on a site that gets lots of 404s: lots of code is executed, many database queries are performed, only to return a 404 for a static file.

Much of these 404s are invisible. They are normally caused by elements of a page that does exist, such as a .css file, a small .png or similar items. In effect, no human ever sees these 404s, only browsers internally.

For Drupal 5.x and 6.x, the following code snippet added to the bottom of your settings.php will reduce resource usage considerably on a busy site.

We measured this on one site and found that the time taken to process and send a 404 from 24 milliseconds, down to 6 milliseconds and even 2 milliseconds in some cases.

On a low traffic site, this will not make much of a difference. However, on a large site with millions of page views per day, this will add up into real savings in terms of CPU, memory and disk I/O.

Here is the snippet that we use in settings.php.

// List of extensions for static files
$exts = 'txt|png|gif|jpe?g|shtml?|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp|xml';

// It is not an imagecache path, which we allow to go through Drupal
if (!strpos($_SERVER['QUERY_STRING'], 'imagecache')) {
  // It is not our main feed page
  if ($_SERVER['QUERY_STRING'] != 'rss.xml') {
    // Is it a static file? 
    if (preg_match('/\.(' . $exts . ')$/', $_SERVER['QUERY_STRING']))
      // Just send a 404 right now ...
      header('HTTP/1.0 404 Not Found');
      print '<html>';
      print '<head><title>404 Not Found</title></head>';
      print '<body><h1>Not Found</h1>';
      print '<p>The requested URL was not found on this server.</p>';
      print '</body></html>';

The ultimate solution though is to get this in Drupal core. Issue #76824, which I submitted more than 4 years ago, aims at doing just that.

Let us try to get this in for Drupal 7.


Fri, 2011/04/01 - 01:49

my website was developed in drupal 6, i trying added this code but some error will showing.. i couldn't analyze the solution. how to solve it?

Mon, 2012/07/30 - 09:05

When running cron via drush, I was getting several PHP notices from settings.php
Undefined index:  QUERY_STRING settings.php:295 [0.08 sec, 5.96MB [notice]

You can avoid these by making the following tweek to the initial code:

Original: if (!strpos($_SERVER['QUERY_STRING'], 'imagecache')) {
Fixed: if (isset($_SERVER['QUERY_STRING']) && !strpos($_SERVER['QUERY_STRING'], 'imagecache')) {


Is your Drupal or Backdrop CMS site slow?
Is it suffering from server resources shortages?
Is it experiencing outages?
Contact us for Drupal or Backdrop CMS Performance Optimization and Tuning Consulting