Make file downloadable without any file extension - apache

I know we can make file downloadable using
AddType application/octect-stream .png
But if I have a file that dont have any extension (FileName) then how I will make that downloadable? or is there anyway to make all file downloadable within a directory?

You can try this directive on your site configuration (apache2):
DefaultType application/octet-stream
And this for nginx:
default_type application/octet-stream;
http://httpd.apache.org/docs/2.2/mod/core.html#defaulttype
https://nginx.org/en/docs/http/ngx_http_core_module.html

Use this in .htaccess is recommended:
<filesMatch "^([^.]+)$">
Header set Content-Disposition "attachment"
</filesMatch>
(This won't work with Chrome, it will append ".txt" to the filename, and it's a known bug)
This will prevent browser from showing the file content inline and let browser download it instead.
Executable file extension like .php (if you've installed), your browser will download the execution result(not the source code), is that what you want?

Related

XAMPP .htaccess AddEncoding br brotli

I install no my MacOs HighSierra fresh XAMPP (osx-7.4.6-0-vm) and start it - server runs on IP 192.168.64.2 . Then I mount /opt/lampp and click to Explore. The Finder window open and I go to directory htdocs/. Then inside htdocs I create folder assets and inside it I copy file alice.wasm (this file is compressed using brotli (br)). You can download this test (brotli compressed) file using this jsfiddle generator. Inside htdocs I create following .htaccess file
<IfModule mod_mime.c>
AddType application/wasm .wasm
AddEncoding br .wasm
AddOutputFilterByType DEFLATE application/wasm
</IfModule>
Problem
When I go to http://192.168.64.2/assets/alice.wasm browser not download file and in chrome>networks tab I see (here are more details)
(failed) net::ERR_CONTENT_DECODING_FAILED
However, when I change AddEncoding br .wasm to AddEncoding rar .wasm (or instead 'br' I use zip, or none) then browser download file (details) but the problem is that browser automatically NOT decompress file (so it save compressed file).
When I put this .htacces file and alice.wasm file to some AZURE apache-like server (but I don't have access to it configuration files) then browser download file and decompress it on the fly (details) - so this is POSSIBLE.
Question: What I should do to make XAMPP working as expected?
brotli module works only on HTTPS mod. See it here: why does not brotli work on http?. If you have ssl certificate, it will work there.
It is very strange, but when I enable port forwarding
And go to http://localhost:8080/assets/alice.wasm then Chrome download file and automatically decompress it (exactly what I want). AZURE works on HTTPS and file was downloaded properly probably because brotli compression is support only for HTTPS - however probably on Chrome localhost is 'special' and allows it too.

How to create direct download link for pdf

I have uploaded a pdf file in ftp server. The following link opens pdf file in the browser.
http://aptform.culinarysuperstars.com/Appointment_static.pdf
How to create a link for pdf file so that it is downloaded directly instead of opening in the browser?
Locate the .htaccess-file on your FTP server. This is usually hidden, so you might have to make sure you can view and edit hidden files. You can then modify it to force downloads instead of showing up in the browser.
Include the following in the file:
AddType application/octet-stream .pdf
I am unsure whether this will work in any browser; if not, try the following in the .htaccess-file:
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Method 2:
<FilesMatch "\.(pdf)$" >
ForceType application/octet-stream
Header add Content-Disposition "attachment"
</FilesMatch>
This is something you can't really control, it's based on browser/user settings.
If you're able to, you can try sending the header Content-disposition: attachment; filename="Appointment_static.pdf" with the file contents, which might trigger a download dialog rather than the file being shown in the browser, but you can't really rely on that.

Apache download files with specific extension

I have download directory on apache webserver. Files in this directory have specific extensions. For example *.yyy and *.zzz. But all these files are renamed .zip or .tar.gz
I've tested in different brawsers and havn't got any problems.
But some users tell that they get source of packages, not download.
I've created.htaccess
AddType application/zip .zzz
AddType application/x-gzip .yyy
But when I try to download aaa.yyy in MS Internet Explorer it try to save not aaa.yyy but aaa.gz
How to force browsers download files and not to change extensions?
<FilesMatch "\.(zzz|yyy)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Force type tell browsers that you dont know file type. So they are not going to do any thing stupid. Second line will tell them to download this file.

Set Content-Disposition header to attachment only on files in a certain directory?

I've got this this rule in my htaccess file to force linked files to download rather than open in the browser:
<FilesMatch "\.(gif|jpe?g|png)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
Is there a way to alter the RegExp so it only applies to files in a certain directory?
Thanks
Like #gumbo said, put the .htaccess file in the highest level folder you want to affect. and those settings will trickle down to sub folders. You may also want to make sure the headers module is enabled before using this in your htaccess file. The following line will generate an error if the headers module is not enabled:
Header set Content-Disposition attachment
here's an example that forces download of mp3 files only if the headers 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 enable the module, it just ignores anything inside the IfModule tags if the module is not enabled.
To enable apache modules you'll either need to edit your httpd.conf file or in wamp server you can click the wamp tray icon and select "Apache -> Apache Modules -> headers_module" or make sure it is checked.
You will probably need to put the directives in the .htaccess file in the particular directory.
Put it in a <Location> directive, and/or modify the regex to exclude slashes or as appropriate.

How to make htaccess set file type on a particular file?

So what I need to do is make it so a html file (without the html extension) gets viewed as an html file.
I had this line:
AddType application/msword mypage
but apparently that last argument in .htaccess only supports extensions. Is there a way to have that thing work on a particular file?
<Files nosuffix>
DefaultType text/html
</Files>
References: DefaultType and Files.