0

I have a server running on Apache 2.4. Clients can upload files, which are only stored compressed. For every file is also created a textfile containing the mimetype of the file.

When a client downloads a file I want the server to read the mimetype out of this textfile and set the HTTP Content-Type Header respectively.

What I have until now: I made a python script, which gets the correct mimetype and writes it in stdout and added these directives to the relevant apache config file

RewriteEngine On     
RewriteMap mimetype prg:/home/user/scripts/mimetype.py
RewriteRule ^(.*)$ - [ENV=CONTENTTYPE:${mimetype:%{REQUEST_FILENAME}}]
Header append Content-Type "%{CONTENTTYPE}e" env=CONTENTTYPE

so the script gets called.

The problem: It only works after service apache2 reload.
But when a new file is uploaded the Content-Type header for the following downloads is empty (even for files, that worked before).

Update: Following the answer of Anson W Han the solution should be using a .htaccess file, because the config file gets only parsed once on service boot/reload.

Unfortunately RewriteMap is not allowed in .htaccess. I tried adding the

RewriteEngine On     
RewriteMap mimetype prg:/home/user/scripts/mimetype.py

directives to the sites config file and placed the .htaccess file containing

RewriteEngine On 
RewriteRule ^(.*)$ - [ENV=CONTENTTYPE:${mimetype:%{REQUEST_FILENAME}}]
Header append Content-Type "%{CONTENTTYPE}e" env=CONTENTTYPE

in the sites Document Root. But that doesn´t work at all. The Content-Type Header is now empty all the time.

htaccess overrides are allowed and http://www.htaccesscheck.com/ says the syntax is ok as does apachectl configtest.

Is there a way to get it working using a .htaccess file?

2 Answers 2

0

It sounds like you placed your Rewrite directive in your site's httpd.conf apache file. This file is only parsed one time on service boot/reload.

If you place the Rewrite directive in the site's .htaccess it'll get hit every time something is called, and thus reflect the latest mime type from your script. To do this, be sure to:

  1. Make sure the apache httpd.conf or equivalent file allows for htaccess overrides.
  2. Confirm whether you want to modify headers early or late in the request. See http://httpd.apache.org/docs/current/mod/mod_headers.html
  3. Add the Rewrite block to an .htaccess file in your webserver's root directory.
  4. Run your htaccess file through a checker to make sure it's valid. (you could use a tool like http://www.htaccesscheck.com/)
  5. Run your updated httpd.conf or equivalent file through an apache config checker (such as the command noted in Command to check validity of Apache server config files)

After applying the updates, don't forget to restart Apache (hopefully for your last time)!

1
  • Thanks for the answer. I followed your steps, but RewriteMap is not allowed in a .htaccess file. I wasn´t able to get it working like this and updated the question.
    – calli.p.
    Nov 27, 2017 at 20:10
0

I finally found the solution: the problem was not the config file itself, but a missing RewriteCond.

Without it the rewrite block was incorrectly executed for file uploads too, which resulted in strange wrong stdin inputs for the python script.

So adding the RewriteCond to the server config file and handling wrong input in the python script adequately fixed this problem for me, even without the use of a .htaccess file.

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.