【发布时间】:2018-12-23 13:36:48
【问题描述】:
图片按文件名的前2个字符在文件夹中,例如stack_help_me.jpg文件将在/mnt/images/st/stack_help_me.jpg文件夹中。
任务是使所有对特定子文件夹图片的请求(例如上传)都重定向到所需的文件。例如,/wp-content/uploads/2018/12/stack_help_me.jpg 到 /mnt/images/st/stack_help_me.jpg。
如何纯粹在.htaccess中做到这一点,有可能吗?
这在 PHP 中似乎是可以理解的(尽管可能与 .htaccess 规则有关的错误)。这是我尝试过的:
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /wp-content/uploads/\.(jpg|jpeg|png|gif)$ [NC]
RewriteRule (.*) navigator.php?&src=$1 [L]
</IfModule>
navigator.php
$real_img_path = '/mnt/images';
$full_path = $_GET['src']; // /wp-
content/uploads/2018/12/toster_help_me.jpg
$only_fname = substr($full_path, strrpos($full_path, '/') + 1); //
toster_help_me.jpg
$image = $real_img_path . '/' . substr($only_fname, 0, 2) . '/' .
$only_fname; // /mnt/images/to/toster_help_me.jpg
fpassthru(fopen($image, 'rb'));
【问题讨论】:
-
是的,现在我知道 htaccess 只能从根目录工作并且只使用 htaccess 是不可能的。
标签: php regex .htaccess mod-rewrite