Apache RewriteRule with a RewriteMap variable substitution for the VAL argument to environment variable

apache-2.2rewriterewritemap

I have an Apache server that serves up binary files to an application (not a browser).

The application making the request wants the HTTP Content-MD5 header in HEX format. The default and only option within Apache is Base64. If I add "ContentDigest on" to my VirtualHost, I get this header in Base64.

So I wrote a perl script, md5digesthex.pl, that gives me exactly what I want: MD5 in HEX format but I'm struggling with the RewriteRule to get my server to send the result.

Here is my current Rewrite recipe:

RewriteEngine on
RewriteMap md5inhex prg:/www/download/md5digesthex.pl
RewriteCond %{REQUEST_URI} ^/download/(.*)
RewriteRule ^(.*) %{REQUEST_URI} [E=HASH:${md5inhex:$1}]
Header set Content-MD5 "%{HASH}e" env=HASH

The problem is that I can't seem to set the HASH environment variable based on the output of the md5inhex map function. It appears this behavior is not supported and I'm at a lost as to how to formulate this…

Best Answer

I think the better question is, why are you redefining what the RFC already defines? If you're trying to overload the Content-MD5 header, chances are you should just be using a different header. If you're trying to adapt this to your program, it sounds to me like your program is doing the wrong thing and should be corrected.

EDIT: So I looked at this locally. Can you tell me what the output of md5digesthex.pl is? It should be a whitespace delimited pair of values, i.e.:

some/path/to/file somehash

It sounds like the key/value may not be matching up, because this WILL do what you want assuming ${md5inhex:$1} executes correctly. What you can try doing, for testing, is setting a dummy variable; i.e:

RewriteEngine on
RewriteMap md5inhex prg:/www/download/md5digesthex.pl
RewriteCond %{REQUEST_URI} ^/download/(.*)
RewriteRule ^(.*) %{REQUEST_URI} [E=HASH:${md5inhex:$1}]
Header set Content-MD5 "%{HASH}e" env=HASH

RewriteRule ^(.*)$ - [E=TEST_HASH:${md5inhex:somekey}]
Header set TestingHeader "%{TEST_HASH}e" env=TEST_HASH

You can use something like curl -I <server>/<path> to verify that its getting set. I can verify that using a correctly formatted RewriteMap, this WILL set your info correctly.