Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

Send 304 Header if cached (Apache)

If a browser sends my server this header
      If-Modified-Since: 1230768000 GMT

If the timestamp is less than one year old I want to respond to the browser with a 304 header and NOTHING else.

I know how to do this with PHP:

However I want to do this ONLY with httpd.conf or .htaccess so it applies to ALL images, documents and other files in a directory, not just PHP scripts.
  • 4
  • 2
1 Solution
That could work with mod_rewrite since apache 2.1 and an external RewriteMap program  (e.g. written in perl) defined in httpd.conf, but a small source code change in mod_rewrite.c is necessary because 304 is considered to be a redirect status code. That means the status code is treated as a redirect response and not a status response. Hence the HTTP body is not empty and a 304+HTTP body is an invalid response. The browser tries to download the HTTP body.

Can you modify mod_rewrite.c and recompile the module?

Another approach might be an apache module written either in c or in perl via mod_perl.
hankknightAuthor Commented:
Many browsers seem to completely ignore the best caching directives and the only foolproof way I have found to prevent them from downloading files again is to send them a 304 header.

A custom apache module is an option for my personal website on my personal server.  But it would not be an option for my many clients who host their websites on a variety of shared servers.  

So the only practical way I know to address this is to use mod_rewrite to cause ALL files and images to be processed by a PHP script that would handle this correctly.  My concern is that the performance gains created by proper usage of the 304 header would be offset by performance losses caused by making PHP process all files and images.

hankknightAuthor Commented:
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

The rewriting to a page in .htaccess files and the processing of a PHP script causes some performance losses, but since this is just a tiny script without any databases involved I think it wouldn't cause too much processing time compared with that th client doesn't need to download large files again.

Serving static files is pretty fast of course with sendfile (EnableSendfile). Until some point the downloading procedure of a small static file might be faster than a 304 issued by a php script.
The approach via a RewriteMap could look like
# httpd.conf
RewriteEngine on
RewriteMap check "/var/www/check.pl"
RewriteCond %{HTTP:If-Modified-Since} ^(.+)
RewriteCond ${time:%1} =1
               # doesn't work until source code modification
RewriteRule ^ - [R=304]
mod_rewrite.c changes (untested)
                    if (!ap_is_HTTP_REDIRECT(status)) {
                    if (!ap_is_HTTP_REDIRECT(status) || status == HTTP_NOT_MODIFIED) {
and the map program check.pl
use DateTime::Format::Mail;
use strict;
use warnings;
$| = 1;
    my $ifsince;
    if ($_ =~ m/^[A-Z]/)
        # oh dear, the client sent a RFC 2822 formatted date 'Thu, 01 Jan 2009 00:00:00 GMT'
        $ifsince = DateTime::Format::Mail->parse_datetime($_);
        $ifsince = $ifsince->epoch;
    } else
        # get rid of ' GMT'
        $ifsince = substr($_,0,-4);
        print $ifsince;
    if (int($ifsince))
        my $time = time();
        # current time - 365.xx days
        $time = int($time)-31556926;
        if ($ifsince<$time)
            print "1\n";
    print "NULL\n";

Open in new window

oh, the map call should be ${check:%1} instead of ${time:%1} and the shebang line should start with #! of course....

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now