Answer the question
In order to leave comments, you need to log in
How to redirect image requests with pure htaccess to another folder?
Good day!
The pictures are in the folder where the folder name is based on the first 2 characters of the file name, for example, the file toster_help_me.jpg will be in the /mnt/images/ to/to
ster_help_me.jpg folder file from a real folder. For example:
/wp-content/uploads/2018/12/toster_help_me.jpg > /mnt/images/to/toster_help_me.jpg etc.
How to do this purely using htaccess and is it possible?
In PHP, it seems to be understandable (although probably with errors regarding htaccess rules).
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /wp-content/uploads/\.(jpg|jpeg|png|gif)$ [NC]
RewriteRule (.*) navigator.php?&src=$1 [L]
</IfModule>
$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'));
Answer the question
In order to leave comments, you need to log in
If they lie outside the DocumentRoot folder (if it is /var/www, for example, and pictures are in /mnt) - then only through a proxy for reading files, as in your example, if they are available somewhere within the web server (for example, in /var /www/mnt), then you can get the first letters like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} /wp-content/uploads/.*?\.(jpe?g|png|gif)$
RewriteRule ^.*/(.{2})(.+?\.(?:jpe?g|png|gif))$ /mnt/$1/$1$2 [NC]
/wp-content/uploads/2018/12/toster_help_me.jpg
to/mnt/to/toster_help_me.jpg
RewriteCond %{REQUEST_URI} /wp-content/uploads/\.(jpg|jpeg|png|gif)$ [NC]
RewriteRule (.*) /wp-content/uploads/2018/12/toster_help_me.jpg [L]
/wp-content/uploads/
will be slipped/wp-content/uploads/2018/12/toster_help_me.jpg
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question