9

I have a directory of text files that I'm serving out with apache 2. Normally when I (or any user) access the files they see them in their browser. I want to 'force'* the web browser to pop up a 'Save as' dialog box. I know this is possible to do with the Content-Disposition headers (more info).

Is there some way to turn that on for each file?

Ideally I'd like something like this:

<Directory textfiles>
   AutoAddContentDispositionHeaders On
</Directory>

And then apache would set the correct content disposition header, including using the same filename.

Something like this might be possible with the apache Header directive.

Bonus points if it's included by standing in apache in debian.

I could do a simple PHP wrapper script that takes in a filename argument, makes the call to header(...) and then prints the file, but then i have to validdate input etc. that's work I'm trying to avoid.


* I know you can't actually force things when it comes to the web

Amandasaurus
  • 31,471
  • 65
  • 192
  • 253

2 Answers2

13

I have discovered that this does what I want:

<Location /textfiles>
    SetEnvIf Request_URI "^.*/([^/]*)$" FILENAME=$1
    Header set "Content-disposition" "attachment; filename=%{FILENAME}e"
    UnsetEnv FILENAME
</Location>
Adrian Heine
  • 328
  • 4
  • 22
Amandasaurus
  • 31,471
  • 65
  • 192
  • 253
  • What is the "e" for at the end of the file name? I see that it doesn't work without it, but I don't understand what it does. – Mark E. Haase Apr 18 '13 at 14:36
  • It just means "the contents of the environment variable named `FILENAME`" (see documentation of mod_headers: https://httpd.apache.org/docs/current/mod/mod_headers.html) – Ale Aug 10 '15 at 18:23
2

mod_headers should be what you are looking for:

<IfModule mod_headers.c> 
  <Location ~ ".*/textfiles/.*"> 
    Header set Content-Disposition attachment
  </Location>
</IfModule>
Christian
  • 4,703
  • 2
  • 24
  • 27
  • A good start, but i also want the filename in there so that it pops up the (same) filename. However I have found a similar solution – Amandasaurus Jan 12 '10 at 16:52