3

I'm using a origin-pull CDN to distributed files quickly right now. It pulls the files from the origin, including the "type" of the file (download/octect-stream right now) and outputs it to the client.

For that I have the following webserver structure:

http://www.server.com/files/code/file.mp4

In the directory "files", I have put a .htaccess file to force a download (otherwise it would just start streaming the video file):

<Files *.*>
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</Files> 

Now, I would like to allow for both downloadable files and streaming files from the same folder. As you can create "aliases" of a folder using the same .htaccess file, I would like to create the following:

http://www.server.com/files/code/file.mp4 -> download
http://www.server.com/streams/code/file.mp4 -> "stream" exactly the same file

How can this be done?

Taapo
  • 145
  • 1
  • 4

1 Answers1

1
ForceType application/octet-stream

It shouldn't be necessary to override the correct Content-Type in order to force a download. Setting the appropriate Content-Disposition header should be sufficient in all modern browsers.

you can create "aliases" of a folder using the same .htaccess file

Yes, sort of. Although you wouldn't be able to use the same .htaccess in the /files directory - if that is what you are implying?

Try the following... remove the .htaccess file in the /files directory and add the following directives to the .htaccess file in the document root (or appropriate subdirectory). These directives may need to go near the top if you have existing directives - the order can be important.

RewriteEngine On

# Force download all direct requests to the ./files directory
RewriteCond %{REDIRECT_STATUS} ^$
RewriteRule ^files/ - [E=FORCE_DOWNLOAD:1,T=application/octet-stream]

# Internally rewrite requests for ./streams to ./files
RewriteRule ^streams/(.+) files/$1 [L]

# Only set the Content-Disposition header when forcing a download
Header set Content-Disposition attachment env=FORCE_DOWNLOAD

The above code does still override the Content-Type when forcing a download (ie. T=application/octet-stream flag on the RewriteRule). Although I believe this should be avoided.

The RewriteCond directive checking against THE_REQUEST REDIRECT_STATUS prevents requests for the ./streams "virtual" directory also being downloaded. It ensures that only "direct" requests for the ./files directory are downloaded.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • I get an error message: "RewriteCond: bad flag delimiters" – Taapo Dec 12 '16 at 22:37
  • Ah yes, sorry, the space should be (backslash) escaped in the _CondPattern_. So, `^GET /files/` should be `^GET\ /files/`. I've updated my answer. (Without the space being escaped, the `/files/` bit is seen as the _flags_ argument, which is invalid.) – MrWhite Dec 12 '16 at 23:27
  • That solved the error. But for some reason it is not working as intended. When I target /subdir/files/3/1/filename.mp4 directly, it's streaming. When I target /subdir/streams/3/1/filename.mp4 directly, it's showing a not found error message. My .htaccess file is located in /subdir/ -- any idea why it's not working? – Taapo Dec 13 '16 at 08:10
  • The above code was intended to work with the URL structure you posted in the question, ie. `/files` and `/streams` both in the document root, not a subdirectory. I've updated my answer and changed the code so it should now work if it is located in either the document root or a subdirectory. (However, this will no longer work if used directly in the server-config, as I had previously stated, but that does not seem to be a requirement here.) – MrWhite Dec 17 '16 at 01:16
  • All worked with this new configuration. Thanks a lot! – Taapo Dec 17 '16 at 05:05