0

I was using all images as webp files for performance, however I've stopped using and deleted them (only .webp files) all from FTP. Now having problems with 404 errors. I would like to redirect all .webp files to jpg, png, gif, etc.

For example:

https://www.example.com/wp-content/uploads/2019/04/file.jpg.webp
https://www.example.com/wp-content/uploads/2019/04/file.jpeg.webp
https://www.example.com/wp-content/uploads/2019/04/file.gif.webp
https://www.example.com/wp-content/uploads/2019/04/file.png.webp

TO...

https://www.example.com/wp-content/uploads/2019/04/file.jpg
https://www.example.com/wp-content/uploads/2019/04/file.jpeg
https://www.example.com/wp-content/uploads/2019/04/file.gif
https://www.example.com/wp-content/uploads/2019/04/file.png

Redirection plugin:

Source URL: ^(?!.*-[0-9]{1,4}x[0-9]{1,4}).*\.(?:jpg.webp)

Target URL: https://www.example.com/wp-content/uploads/$1/$2/$3.$4

And it didn't work. Also, I tried it with htaccess but again, it didn't work. Is there a way to arrange that?

1
  • Browsers will try to reach the files directly, bypassing WP. You'll need to solve this on the server (.htaccess) level. – cjbj Feb 19 at 10:56
1

If I'm reading it right, your regex has only two items in (), which means that only $1 and $2 have any meaning.

I copied your sample text and went to https://regex101.com/ (note: this is not an endorsement) to try to assemble a regex that'll cover all your images. Here's what I ended up with:

Pattern: ^(.*\/[0-9]{1,4}\/[0-9]{1,2}\/)([^\.]+)\.(jpg|jpeg|gif|png)\.webp
Replacement: $1$2.$3

This is using the PCRE standard, which is apparently used in PHP < 7.3. You may need to tweak the regex if you're using PHP 7.3 or 7.4 (or higher), or if you want to use it in a .htaccess file.

1
  • And works like a charm. Thank you! – Eagle Feb 18 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.