1

I am working on a web application that need to serve a number of continuously updated documents at static URLs, but the Content-Type of the documents differs (it's either JPEG or SVG). If the document is a symbolic link, the file at the end of the link has the correct file extension for MIME type mapping, but I have not been able to tell if it is possible to get Apache to follow the link before looking the extension up in the MIME table.

This question asks the same thing, but the workaround provided doesn't work for me, as browsers do not autodetect SVG if I serve image/jpeg (they do autodetect GIF and PNG), so I need a proper MIME type.

Normally, I would use .meta files and mod_cern_meta for this, but my target Linux distributions (Fedora 19/20, RHEL 7) does not come with this, and I would like to avoid having to supply it myself. I don't think I can use mod_headers, since it would require me to rewrite the entire .htaccess file (the files are changed on an individual basis), nor mod_asis, as the data files themselves are generated using a third-party tool.

Edit: I am working around this by writing my files as type-maps (with one entry only), pointing to the actual resource and listing its Content-Type. It means having to write extra files, but so would using mod_meta. Works well enough for the time being.

2 Answers 2

0

have you tried mod_mime_magic? with this module enabled, apache can guess the content type the same way "file" command does and will set the content-type header correctly.

Checking the default configuration for a centos httpd installation, mod_mime_magic seems to be enabled by default and the content-type is set correctly when (for example) the request targets a symlink (namely "aaaa") for a, let's say, .png file.

Also, make sure you have followsymlinks option enabled for the root directory containing the symbolic links (or just enable it for the entire documentroot)

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
[...]

I'm going to include for brevity some configuration hints for apache httpd to make mime magic work:

#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>
1
  • Thanks for the tip! It seems that I would have to enable it for the entire server, not only just for the directory where I have the troublesome files (or, for that matter, the files themselves), so I'm not sure if it will work, but I will give it a shot at least.
    – nafmo
    Jan 15, 2014 at 15:03
0

How about making your static URLs not point directly to the documents, but to a simple script that sets the correct Content-Type header and then streams the document?

<?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);
?>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .