Apache2:使用符号链接的目标名称进行Content-Type映射

我正在开发一个Web应用程序,它需要在静态URL上提供大量持续更新的文档,但文档的内容types不同(它可能是JPEG或SVG)。 如果文档是符号链接,则链接末尾的文件具有用于MIMEtypes映射的正确文件扩展名,但是在查看扩展之前,我无法确定是否有可能让Apache遵循链接在MIME表中。

这个问题提出了同样的问题,但是提供的解决方法对我不起作用,因为如果我提供image / jpeg(它们自动检测GIF和PNG),浏览器不会自动检测SVG,所以我需要一个适当的MIMEtypes。

通常情况下,我会使用.meta文件和mod_cern_meta ,但是我的目标Linux发行版(Fedora 19/20,RHEL 7)没有这个function,我想避免自己提供。 我不认为我可以使用mod_headers,因为它需要我重写整个.htaccess文件(文件被单独更改),也不需要mod_asis,因为数据文件本身是使用第三方工具生成的。

编辑 :我正在通过编写我的文件作为types映射(只有一个条目),指向实际资源并列出其内容types。 这意味着必须编写额外的文件,但使用mod_meta也是如此。 暂时工作得不错。

如何使您的静态URL不直接指向文档,而是一个简单的脚本,设置正确的Content-Type标题,然后stream式处理文档?

<?php $filename = "path/to/your/file"; $finfo = finfo_open(FILEINFO_MIME_TYPE); $mimetype = finfo_file($finfo, $filename); finfo_close($finfo); header("Content-Type: ".$mimetype ); echo readfile($filename); ?> 

你有没有尝试过mod_mime_magic ? 启用此模块后,apache可以按照“文件”命令的相同方式猜测内容types,并将正确设置内容types标题。

检查一个centos httpd安装的默认configuration,mod_mime_magic似乎是默认启用的,并且当(例如)请求针对a的符号链接(即“aaaa”)时,内容types被正确设置,比方说,.png文件。

此外,请确保您为包含符号链接的根目录启用了followymlinks选项(或者只为整个文档启用它)

 lrwxrwxrwx 1 root root 10 Jan 15 16:27 aaa -> pgid35.png -rw-r--r-- 1 root root 229727 Jan 15 16:26 pgid35.png HEAD http://localhost/tt/aaa 200 OK Connection: close Date: Wed, 15 Jan 2014 13:27:30 GMT [...] Content-Length: 229727 Content-Type: image/png <<<--- [[bullseye]] Last-Modified: Wed, 15 Jan 2014 13:26:47 GMT [...] 

为了简洁起见,我将包含一些Apache httpd的configuration提示,以使MIME的工作成为可能:

 #assuming the module folder is linked within apache root config dir as 'modules' LoadModule mime_magic_module modules/mod_mime_magic.so #define the magic file containing patterns for identifying file types MIMEMagicFile conf/magic #the system magic file normally contains more patterns than #the default http magic file #centos magic file install path: #MIMEMagicFile /usr/share/misc/magic #do follow the symlinks if you are using them <Directory /> Options FollowSymlinks </Directory>