最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - How to redirect Webp files?

programmeradmin2浏览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:

.jpg.webp
.jpeg.webp
.gif.webp
.png.webp

TO...

.jpg
.jpeg
.gif
.png

Redirection plugin:

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

Target URL: /$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?

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/wp-content/uploads/2019/04/file.jpg.webp
https://www.example/wp-content/uploads/2019/04/file.jpeg.webp
https://www.example/wp-content/uploads/2019/04/file.gif.webp
https://www.example/wp-content/uploads/2019/04/file.png.webp

TO...

https://www.example/wp-content/uploads/2019/04/file.jpg
https://www.example/wp-content/uploads/2019/04/file.jpeg
https://www.example/wp-content/uploads/2019/04/file.gif
https://www.example/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/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?

Share Improve this question asked Feb 18, 2021 at 2:58 user179248user179248 1
  • Browsers will try to reach the files directly, bypassing WP. You'll need to solve this on the server (.htaccess) level. – cjbj Commented Feb 19, 2021 at 10:56
Add a comment  | 

1 Answer 1

Reset to default 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/ (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.

发布评论

评论列表(0)

  1. 暂无评论