Apache: Setting the Content-Disposition Header with mod_rewrite

| No TrackBacks
firefox-cappuccino-kolich.jpgFollowing yesterday's post on serving up static content with Restlet's built in web-server, I received a couple of questions on Twitter inquiring how I implemented my "download the latest Cappuccino" feature at http://mark.koli.ch/cappuccino/latest.  If you visit http://mark.koli.ch/cappuccino/latest you'll notice it prompts you to save a file named "cappuccino.jar", which is the latest Cappuccino build available on kolich.com.  Even if I change versions, and post a newer build on my server, you can always download the latest from http://mark.koli.ch/cappuccino/latest.  So, you might expect this URL to prompt you to save a file named "latest", given that this is how most web-browsers work; they take the string following the last / and use that as the file name.  However, when someone visits /latest I want the browser to prompt them to save "cappuccino.jar" and not "latest".

The title of this post is slightly misleading, since the Content-Disposition header cannot be set directly using mod_rewrite.  As I understand it, there are a set of headers that mod_rewrite understands, and Content-Disposition is not one of them.

So, to prompt the user to save cappuccino.jar instead of "latest", I used an interesting combination of mod_rewrite and mod_headers.  Specifically, I used mod_rewrite to internally redirect any requests to .../cappuccino/latest to the latest cappuccino Jar file available on my server.  Second, I used mod_headers to add a Content-Disposition header to any request ending with ../cappuccino/latest, which forces the browser/client to interpret the file as "cappuccino.jar" and not "latest".  Here's the configuration from my Apache httpd.conf file:

## Internally redirect for Cappuccino
RewriteRule ^/cappuccino/latest /cappuccino/dist/cappuccino-v0.1.jar [L]
SetEnvIfNoCase Request_URI cappuccino\/latest$ cappuccino-latest
Header set Content-Disposition \
"attachment; filename=cappuccino.jar" env=cappuccino-latest

With this configuration, Apache will set the Content-Disposition header on each request for http://mark.koli.ch/cappuccino/latest.  Using a tool like HttpFox, you can prove to yourself that this works as expected:

httpfox-snapshot-cappuccino-kolich.jpg


Enjoy.

Did You Find this Helpful?

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

  

Send Mark a Direct Message

If you'd like to send me a direct message, please do so below. However, I do not publicly post comments or messages submitted directly to me. So, if you're going to try to SPAM me, or my blog, you're pretty much wasting your time.

400 characters remaining

Error

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer, a casual entrepreneur, and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

No TrackBacks

No trackbacks attached to this entry.

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on September 24, 2009 8:34 AM.

Cappuccino: A Really Simple Web-Server Powered By Restlet was the previous entry in this blog.

Recently Acquired Koli.ch ... Blog Moving Soon is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.