Using only .htaccess is not possible, this is because there are several types of image and it would be complicated to add mime-type to each one, however you can redirect to a php handle, for example:
Create a .htaccess: file with this content in the image folder:
RewriteEngine On
RewriteRule ^ proxy_image.php?path=$0 [L]
In the same folder, create a file named proxy_image.php
with the following content:
<?php
function mimeType($file)
{
$mimetype = false;
if (class_exists('finfo')) {//PHP5.4+
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $file);
finfo_close($finfo);
} else if (function_exists('mime_content_type')) {//php5.3 ou inferiror
$mimetype = mime_content_type($file);
}
return $mimetype;
}
if (empty($_GET['path'])) {
echo 'Caminho invalido';
exit;
}
$path = $_GET['path'];
$extensoes = array('jpeg', 'jpg', 'gif', 'png', 'ico', 'svg');
$fullPath = null;
//Busca pela primeiro arquivo que contiver a extensão (pode implementar glob também, é opicional)
foreach ($extensoes as $extensao) {
if (is_file($path . '.' . $extensao)) {
$fullPath = $path . '.' . $extensao;
break;
}
}
if ($fullPath) {
//pega o mime-type do arquivo
$mime = mimeType($fullPath);
//Verifica se o mime-type é do tipo imagem
if (strpos($mime, 'image/') === 0) {
header('Content-Type: ' . $mime);
readfile($path);
exit;
}
}
echo 'Isto não é uma imagem';
exit;
Then just navigate to something like http://localhost/images/foobar
, if there is an image with this
Note that it is possible to implement caching and use Etag along with this code above, as I used in this example of the question (not the answer):