大网数据广告活动
服务器运维问答

Using .htaccess "folder" rules to force different content-disposition types?

· 52CE 我爱测

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 followi…

问题原文

原帖链接 · Taapo · CC BY-SA 3.0

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?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-12-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。