views:

433

answers:

2

Hello,

I'm using Apache's mod_rewrite to route requests for .jpg files to a directory outside my web root.

It generally has been fine, but there are a few images that do not display. I then realized that when I use PHP's get_headers() function on my image URLs, they are all returning "Content-Type: text/html; charset=UTF-8" instead of the proper image/jpeg header types.

I have tried explicitly setting the "Content-Type: image/jpeg" header and still, none of my images return the correct headers - although most do display correctly, but I'm not sure why.

How can I assure a JPG file is sent with the correct header when redirecting via mod_rewrite?

Thanks, Brian

+2  A: 

This is what you could do. Create a PHP file that will get the right file and passes it through

<?php 
$sImage = 'imagename.jpg';
header("Content-Type: image/jpeg");
header("Content-Length: " .(string)(filesize($sImage)) );

echo file_get_contents($sImage);

or

<?php
$sImage = 'imagename.jpg';
$rFP = fopen($sImage, 'rb');

header("Content-Type: image/jpeg");
header("Content-Length: " .(string)(filesize($sImage)) );

fpassthru($rFP);
exit;
Robert Cabri
`fpassthru()` is a better option, you don't need to load the whole file into memory.
Lukáš Lalinský
Thanks, I did wind up specifying the headers using PHP with the fpassthru function.
Brian
+4  A: 

You can also set the Content-Type header field with mod_rewrite with the T flag:

RewriteRule … … [T=image/jpeg]
Gumbo
Thanks very much, very helpful.
Brian