Set the Content-Disposition header to an attachment only on files in a specific directory?

I have this rule in my htaccess file to make related files load rather than open in a browser:

<FilesMatch "\.(gif|jpe?g|png)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch> 

Is there a way to change RegExp , so it only applies to files in a specific directory?

thank

+14
header apache .htaccess
Oct 20 '10 at 11:16
source share
3 answers

You will probably need to put the directives in the .htaccess file in a specific directory.

+12
Oct. 20 2018-10-10
source share

Like @gumbo, put the .htaccess file in the highest level folder you want to influence. and these settings will flock to subfolders. You can also make sure the headers module is enabled before using it in your htaccess file. The following line generates an error if the header module is not enabled:

 Header set Content-Disposition attachment 

Here is an example that forces you to download mp3 files only if the header module is enabled:

 <IfModule mod_headers.c> <FilesMatch "\.(mp3|MP3)$"> ForceType audio/mpeg Header set Content-Disposition "attachment" Allow from all </FilesMatch> </IfModule> 

Note: it does not include the module, it simply ignores anything inside the IfModule tags if the module is not included.

To enable apache modules, you will need to either edit the httpd.conf file or on the Wamp server, you can click the icon and select "Apache β†’ Apache Modules β†’ headers_module" or make sure it is installed.

+9
Jan 28 '13 at 21:26
source share

Put it in the <Location> directive and / or change the regular expression to exclude slashes or, if necessary.

+2
Oct. 20 '10 at 11:27
source share



All Articles