Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wappz's avatar
Level 2

htaccess rewrite jpg to webp in storage folder

Hi there,

I upload a jpg/png to the storage/app/public/images folder in a Laravel project and create a webp image of the uploaded file. I want to rewrite the jpg/png file to the webp file, without creating an url. I've tried a lot, but it doesn't work. The storage symlink is made in the public folder.

Has someone the solution? Thanks in advance!

Mike

0 likes
6 replies
jlrdw's avatar

What is webp file. Are you wanting to just copy an image to another folder. Please explain more.

Sinnbeck's avatar

I've tried a lot, but it doesn't work.

What have you tried? And I am a bit confused. You want to call htto://yoursite.com/image.jpg and get a webp file back?

@jlrdw webp is just a new super optimized image format for web. Like jpg but smaller, and specifically designed for web

wappz's avatar
Level 2

I've tried this for example:

RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/storage/app/public/images/.jpg.webp -f
RewriteCond %{HTTP_HOST}@@%{HTTP_REFERER} ^([^@]*)@@https?:///.*
RewriteRule (.+)\.jpg$ /storage/images/.jpg.webp [NC,T=image/webp,E=cache-control:no-cache,L]

That's right, I want the webp version of image.jpg. With htaccess you can rewirte that. I copied that from a wordpress plugin called webp-express.

The htaccess of the uploads folder in wordpress is:

  # Redirect to existing converted image in cache-dir (if browser supports webp)
  RewriteCond %{HTTP_ACCEPT} image/webp
  RewriteCond %{REQUEST_FILENAME} -f
  RewriteCond %{REQUEST_FILENAME} (?i)(/home/site/domains/site.nl/public_html/wp-content/uploads/)(.*)(\.jpe?g|\.png)$
  RewriteCond /home/site/domains/site.nl/public_html/wp-content/webp-express/webp-images/uploads/%2%3.webp -f
  RewriteRule (?i)(.*)(\.jpe?g|\.png)$ /wp-content/webp-express/webp-images/uploads/%2%3\.webp [T=image/webp,E=EXISTING:1,E=ADDVARY:1,L]

This plugin creates a webp-express folder in the wp-content folder and adds the webp files in this folder (organised by year, as wordpress does too).

I hope you can follow my example!

wappz's avatar
Level 2

@jlrdw How should this help me? I don't want to convert the images, that's already working. I want the webp files if I load a jpg file, so that I don't need to load the webp url. It can as webp-express show, I can't figure out how with htaccess.

Please or to participate in this conversation.