B
B
Bone2021-10-19 15:02:14
Apache HTTP Server
Bone, 2021-10-19 15:02:14

How to make a redirect to a file in .htaccess, taking into account the query string?

Hello! The essence of the task is to return reduced versions of pictures, if they exist, and if not, then redirect the request to a script that will make a reduced version.

How it works:
1. The /original folder contains original images, for example /original/1.jpg
2. To reduce images, it is suggested to use the /resized/ prefix and GET parameters. Like this: /resized/original/1.jpg?w=100&h=100
3. It is supposed that if the file /resized/original/1_100_100.jpg exists, then give it away, if it does not exist, then redirect to /resizer.php (RewriteRule ^(.*)$ /resizer.php?path=$1 [NC,L,QSA])

Now the rezised folder contains the following .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /resizer.php?path=$1 [NC,L,QSA]
</IfModule>

The question is how to check for the existence (and return it if it exists) of the file /resized/original/1_100_100.jpg if the request looks like /resized/original/1.jpg?w=100&h=100

Thanks!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2021-10-19
@Bone

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond &%{QUERY_STRING} ^(?=.*&w=(\d+))(?=.*&h=(\d+))
RewriteCond %{DOCUMENT_ROOT}/resized/$1_%1_%2.$2 -f
RewriteRule ^(original/.+)\.(.+) /resized/$1_%1_%2.$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /resizer.php?path=$1 [L,QSA]
</IfModule>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question