Wednesday, July 1, 2009

returning apache 204

"Can you return an HTTP 204 (no content) response from vanilla apache, from within the conf file? For any request that matches a regex?" asked my coworker. It seemed simple enough.

A quick search showed people building little cgi scripts to fork off. That seemed like wasteful overhead, especially since this was basically to be used as a kill switch to lessen a potential data flood.

Ask Apache has a nice list of all available HTTP response codes and had a link to mod_alias redirect, which is right next to the docs for RedirectMatch

Other status codes can be returned by giving the numeric status code as the value of status. If the status is between 300 and 399, the URL argument must be present, otherwise it must be omitted. Note that the status must be known to the Apache code (see the function send_error_response in http_protocol.c).
Putting together the Redirect documentation with the RedirectMatch documentation, we see that yes, we can send a custom 204 back. Here is a quick example that responds with 204 to any request matching foo with any characters after it.
#RedirectMatch example using mod_alias
RedirectMatch 204 foo(.*)$

His next question was more difficult, "If we're using rewrite rules and redirect match, which one 'wins' if they both match the same url? We're using [L] in rewrite rules to force matches". That one I'm still wondering about.

Redirect directives do take precedence over other mod_alias directives, like Alias and ScriptAlias:

Note: Redirect directives take precedence over Alias and ScriptAlias directives, irrespective of their ordering in the configuration file.


Neal said...

Solve this one:

Chris Short said...

For whatever reason I can never get this to work effectively with URLs ending in .html. Any ideas?