使用.htaccess“文件夹”规则强制不同的内容处置types?

我现在正在使用原始CDN来快速分发文件。 它从源文件中提取文件,包括文件的“types”(现在下载/ octect-stream)并将其输出到客户端。

为此,我有以下的networking服务器结构:

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

在目录“文件”中,我已经把一个.htaccess文件强制下载(否则它将开始stream式传输video文件):

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

现在,我想同时允许来自同一个文件夹的可下载文件和stream文件。 正如你可以使用相同的.htaccess文件创build一个文件夹的“别名”,我想创build以下内容:

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

如何才能做到这一点?

 ForceType application/octet-stream 

不需要重写正确的 Content-Type来强制下载。 在所有现代浏览器中设置适当的Content-Disposition标题就足够了。

您可以使用相同的.htaccess文件创build文件夹的“别名”

是的,有点。 虽然你不能在/files目录中使用相同的.htaccess ,如果这就是你所暗示的?

请尝试以下操作:删除/files目录中的.htaccess文件,并将以下指令添加到文档根目录(或适当的子目录)中的.htaccess文件中。 如果您有现有的指令,这些指令可能需要靠近顶端 – 顺序可能很重要。

 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 

上面的代码在强制下载时仍然覆盖了Content-Type (例如, RewriteRule上的T=application/octet-stream标志)。 虽然我相信这应该避免。

RewriteCond指令检查 THE_REQUEST REDIRECT_STATUS可以防止下载./streams “虚拟”目录的请求。 它确保只下载对./files目录的“直接”请求。