Search code examples
phpimage.htaccessmod-rewriteoptimization

How to read parameters from URL that ends with .jpg


I have a script called image.php that does some image manipulation like resizing and optimization. I already wrote .htaccess to omit the .php at the end and now I have something like this:

http://www.example.com/images/imageFolderPath/myImage.jpg?width=100&keep_aspect=1&quality=80

where the parameters were supposed to be passed to images.php and be used to manipulate imageFolderPath/myImage.jpg.

My problem is I don't know how to write .htaccess that would read the imageFolderPath/myImage.jpg as a parameter and also pass along other parameters if exist.

I got the whole idea from AirBnB website, how they manipulate their images like this

https://a1.muscache.com/ic/discover/94?interpolation=lanczos-none&output-format=jpg&output-quality=70&v=33b4f2&downsize=326px:326px

This is my .htaccess so far:

#RewriteOptions inherit
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1

Solution

  • Insert this rule just below RewriteEngine On line:

    RewriteRule ^images/(.+)\.(jpe?g|png|gif)$ images.php?image_path=$1.$2 [L,QSA,NC,R=302]