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?

1
  • 1
    can you update the Apache config or are you forced to use htaccess – Jacob Evans Dec 10 '16 at 20:30
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.

5
  • 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 8: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 1:16
  • All worked with this new configuration. Thanks a lot! – Taapo Dec 17 '16 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.