When dealing with AJAX, you might need to configure Apache to return a HTTP 204 (No Content). This is useful when your AJAX scripts need to "ping" the server, but you don't want the server to actually return any data in the response (e.g., just acknowledge the request). The server might do something behind the scenes though (like log the request) before it returns a 204. As I understand it, the only difference between a 200 and a 204, is that a 204 response means that "the server has fulfilled the request but does not need to return an
entity-body".I tried to figure out how to configure Apache to return a 204 No Content using one of the built in modules, like mod_actions, mod_alias, or mod_headers.
Solution #1: Use RedirectMatch
As explained by Andrew Grangaard, you can use Apache's RedirectMatch directive to trigger on a specific URL pattern and reply with an HTTP 204 No Content response:
RedirectMatch 204 tracker(.*)$
This triggers on any URI starting with "tracker".
Solution #2: Use a Perl Script
You could also use Apache's SetHandler and Action directives to intercept the request and then call a Perl/CGI script to return a 204 No Content. In my kolich.com VirtualHost configuration, I used the Location directive to define the URI I wanted Apache to trigger on:
<Location /tracker>
SetHandler nocontent-handler
Action nocontent-handler /gen_204.cgi virtual
</Location>
So this tells Apache to call gen_204.cgi each time a request comes starting with /tracker. For example, if you visit http://mark.koli.ch/tracker/heydude then your browser will get a HTTP 204 response back. Note the "virtual" modifier at the end of the Action directive. Defining the action as "virtual" is important because it tells Apache not to check if the requested file/resource actually exists. In the example above, /tracker/heydude doesn't actually exist (it's not a real resource on the server), so if you don't use the "virtual" modifier, Apache will return a 404 Not Found instead of calling the gen_204.cgi script.
Here's the gen_204.cgi Perl/CGI script I'm using that returns a 204 No Content. This is called by Apache anytime a request comes in starting with /tracker:
#!/usr/bin/perl -w
use strict;
use warnings;
use CGI;
my $cgi = CGI->new();
print $cgi->header('text/html','204 No Content');
exit;
This seems to work very nicely.
Mark, why are you doing this?
I configured my blog's core JavaScript to "ping" kolich.com every time someone uses my Translate, Twitter, or Sharing widgets. In other words, each time someone clicks a link in these widgets, my core JavaScript sends an AJAX request to the server to log the click. This is so I can run reports against my Apache logs to see how many times folks actually used these widgets.
FWIW, Google actually does something similar. Each time a user clicks on a link in their search results, Google pings itself to record the click; the response code from the ping is an HTTP 204. Here's a screenshot showing the Google ping.


Did you find this post helpful, or at least, interesting?