Force file download from specific directory .htaccess - .htaccess

So I found the following which will force an image to download instead of open in the browser
<FilesMatch "\.(jpg)$">
Header set Content-Type application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Is there a way to apply this only to one directory? I tried
<FilesMatch "wallpaper\.(jpg)$">
With no luck. Basically I only want files in that specific folder (not subdirectories) to force a download if someone browses to it.

I have not tried this, but I think you could use a Directory directive in your .htaccess file to accomplish this:
<Directory "wallpapers">
<FilesMatch "\.(jpg)$">
Header set Content-Type application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
</Directory>
From the documentation:
< Directory > and < Directory/ > are used to enclose a group of directives
that will apply only to the named directory, sub-directories of that
directory, and the files within the respective directories.

Related

Access files outside of webroot through alias and .htaccess rule

I need to make PDF files that are stored in a folder (with subfolders) outside of the web root publically accessible by a plain URL. An alias has been created in Apache that leads this folder so what I need now is a redirect rule in .htaccess to make this work.
I have this alias: https://www.examplesite.com/certificate
The URLs that will be used to access these PDFs are for example: https://www.examplesite.com/certificate/2018/LGOIGD9E9345034GJERGJER.PDF
https://www.examplesite.com/certificate/2017/GSDFJGLKJNL345L34LSNFLSD.PDF
How should I format my redirect rule in .htaccess to decide if the file is to be downloaded or viewed in the browser?
Sorry about the noise, I found the answer by myself:
<FilesMatch "\.pdf$">
ForceType applicaton/octet-stream
Header set Content-Disposition attachment
</FilesMatch>

How to Force Download files via .htaccess

I am trying to make my site "www.suruleretv.co" files like mp3, mp4 to force download instead of streaming.
I've tried to add some codes in find online via .htaccess but am getting error after adding it.
<FilesMatch "\.mp3$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
it would help if you could add your code to the question, to clarify what you want and to identify what the issue is.
However, if you want to force files to be downloaded, adding the following in your .htaccess should be enough:
<FilesMatch "\.(mp3|mp4)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Please keep in mind, that if you only add this part and have nothing else in it, you will have to save your .htaccess file in the same directory as the downloadeable files. It won't work for subfolders, without adding more rules.

File only to e downloaded on specific page

I have a website with files that should only be downloaded from the download.php file, their saved in a map like /uploads/map1_/file.bin
I don't want people to be able to download the file directly from the directory but only from the download page. I think this is possible with htacces, but I can't find how to do that..
First, create a .htaccess file in your /uploads/ folder.
Then, put this code into it
<FilesMatch "\.bin$">
Order Allow,Deny
Deny from All
</FilesMatch>
Options -Indexes
Note: the most secure solution is to put your bin files out of public scope

Forcing a download using <filesMatch> in htaccess

I'm trying to force the download of all files of one folder.
The link on the page looks like this
Click to download
And I have this snippet in my .htaccess
<filesMatch ".*uploads/documents.*">
ForceType application/octet-stream
Header set Content-Disposition attachment
</filesMatch>
I already know that the 2 lines inside the tag works, because it works when I put a .htaccess directly inside the folder where I want to force the download with the following code:
<Files *.*>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
There seems to be something which I don't understand about the filesMatch tag.
Searching more info found this code:
<FilesMatch "\.(mov|mp3|jpg|pdf|mp4|avi|wmv)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Worked for me.
Please look at the documentation for FilesMatch and Files, respectively. It clearly states
The directives given within this section will be applied to any object
with a basename (last component of filename) matching the specified
filename.
That means that in your example it matches against file.pdf. Your second example *.* matches file.pdf, however your first example .*uploads/documents.* does not. It actually can never match, since it contains a slash, which is used as a directory separator.
If you can edit the apache config
You should enclose either <Files *.*> or <Files *.pdf> (depending on what you want to enforce downloading) in a Location directive:
<Location "/uploads/documents/">
<Files *.*>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
</Location>
If you cannot edit the apache config
Unfortunately, the Location directive is not allowed inside .htaccess files. Just create a .htaccess inside your /uploads/documents/ directory.
This code is perfect if you don't use - in the file name!
For example, for name-1.mp3, change to name1.mp3
<FilesMatch "\.(mp3|avi)$" >
ForceType application/octet-stream
Header add Content-Disposition "attachment"
</FilesMatch>
Clear your browser and check it.

How do I force the browser download all file types in a directory using .htaccess?

How would I tell the browser to force download all file types in a directory instead of rendering them inside the browser?
I've tried this but it didn't work.
<Files *.*>
ForceType applicaton/octet-stream
</Files>
This would be better tasked with:
<Files *.*>
Header set Content-Disposition attachment
</Files>
I use this with a FilesMatch instead of Files. Below is an example with text-based files
<FilesMatch "\.(?i:doc|odf|pdf|rtf|txt)$">
Header set Content-Disposition attachment
</FilesMatch>
I'm not sure that is possible. But you can use a mod_rewrite to a .php file with the file name in the GET part of the URL. Then extract the file name and force a download through that. If you want some download code, just ask

Resources