Send 304 Header if cached (Apache)

Posted on 2009-02-23
Last Modified: 2012-05-06
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.
Question by:hankknight
    LVL 27

    Accepted 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.
    LVL 16

    Author Comment

    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.

    LVL 16

    Author Comment

    LVL 27

    Expert Comment

    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.
    LVL 27

    Expert Comment

    The approach via a RewriteMap could look like
    # httpd.conf
    RewriteEngine on
    RewriteMap check "/var/www/"
    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
    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

    LVL 27

    Expert Comment

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

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
    If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now