Faster 404s with Drupal and ImageCache

Drupal generates nicely styled 404 pages that are easy to customize. And since 404 responses can be served from the page cache, the performance hit from the occasional stray image can be minimal.

However, if you're embedding 3rd-party content (such as ads or social widgets), there is an additional risk that a misbehaving app can generate bogus requests with random-ish query strings. The unpredictable URLs will totally defeat the page cache, so on a really busy site the added load can be crushing.

On Emmys.com we added a few extra rewrite rules to handle this possibility, taking care to make sure Drupal still handled imagecache requests:

These directives replace the main ErrorDocument and RewriteRule settings in .htaccess.

# Use default 404 handler
ErrorDocument 404 default
 
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /imagecache/ [OR]
RewriteCond %{REQUEST_URI} !\.(png|gif|jpe?g|css|js|cgi|ico|xml|swf|flv|dll|exe)$ [NC]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Filed under:

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Ready for transformation?