Viết lại để một kịch bản CGI là lựa chọn duy nhất của bạn từ một .htaccess, về mặt kỹ thuật bạn có thể sử dụng một programatic RewriteMap với một RewriteRule trong một httpd.conf tập tin.
Tập lệnh có thể phục vụ tệp trực tiếp, vì vậy với việc viết lại nội bộ, logic có thể hoàn toàn là phía máy chủ, ví dụ:
.htaccess Rule
RewriteEngine On
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^images/(.*)$ /getLatest.php [L]
đâu getLatest.php là một cái gì đó như:
<?php
$dir = "/srv/www/images";
$pattern = '/\.(jpg|jpeg|png|gif)$/';
$newstamp = 0;
$newname = "";
if ($handle = opendir($dir)) {
while (false !== ($fname = readdir($handle))) {
// Eliminate current directory, parent directory
if (preg_match('/^\.{1,2}$/',$fname)) continue;
// Eliminate all but the permitted file types
if (! preg_match($pattern,$fname)) continue;
$timedat = filemtime("$dir/$fname");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fname;
}
}
}
closedir ($handle);
$filepath="$dir/$newname";
$etag = md5_file($filepath);
header("Content-type: image/jpeg");
header('Content-Length: ' . filesize($filepath));
header("Accept-Ranges: bytes");
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $newstamp)." GMT");
header("Etag: $etag");
readfile($filepath);
?>
Lưu ý: Mã một phần vay mượn từ các câu trả lời trong: PHP: Get the Latest File Addition in a Directory
có thể không làm cho bạn một kịch bản (nói, PHP) mà chuyển hướng đến tập tin mới nhất và sử dụng RewriteCond để đi đến đó? – mgarciaisaia
Tôi cho rằng tôi có thể, nhưng tôi đang tìm giải pháp nhanh nhất có thể và tạo các tập lệnh php có nghĩa là giới thiệu chuyển hướng thứ hai. Tôi đang nghĩ về giải pháp với các liên kết tượng trưng đến tệp mới nhất trong mỗi thư mục –