Matthieu Herdegen
Matthieu Herdegen

Reputation: 11

htaccess image size parameters

I'm struggling with htaccess and rewriteRule

I want to change the filename if I there is "w" parameters and it's an image.

original url : image.jpg?w=500

redirect : image-500.jpg

I tried something like that, but it seems to only remove all the images :

RewriteCond %{REQUEST_URI} ^/(.*)\.[jpg|png|jpeg|gif]
RewriteRule ^([^\.]+)\-([^\.]+)\.(png|jpg|gif)$ /$1.$3?w=$2 [NC,L]

Upvotes: 0

Views: 189

Answers (1)

Matthieu Herdegen
Matthieu Herdegen

Reputation: 11

Found the solution :

RewriteCond %{REQUEST_URI} (\.gif|\.jpg|\.png|\.jpeg)
RewriteCond %{QUERY_STRING} ^(.*)&?w=(.*)?(.*)$ [NC]
RewriteRule ^/?(.*)\.(.*)$ /$1-%2.$2?%1 [R=301,L]

Upvotes: 0

Related Questions