Htaccess将Modula Gallery图像文件名重新写入LiteSpeed缓存的图像

问题描述

我正在使用适用于wordpress的Modula Pro图片插件,并且加载的所有缩略图均具有以下任一URL格式:

https://example.com/wp-content/uploads/sites/3/2020/08/filename-300x99999.jpg
https://example.com/wp-content/uploads/sites/3/2020/08/filename-99999x300.jpg

我还在使用LiteSpeed缓存,它使用以下URL格式生成的图像大约小3-4倍:

https://example.com/wp-content/uploads/sites/3/2020/08/filename-300x168.jpg
https://example.com/wp-content/uploads/sites/3/2020/08/filename-168x300.jpg

我尝试将其添加到htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^(.+)-99999x300.jpg $1-169x300.jpg [QSA,L]
  RewriteRule ^(.+)-300x99999.jpg $1-300x169.jpg [QSA,L]
</IfModule>

我在做什么错了?

解决方法

以下是有效的方法。由于我使用的是OpenLiteSpeed服务器,因此需要用斜杠使其起作用:

RewriteRule /(.+)-300x99999.jpg /$1-300x169.jpg [QSA,L]
RewriteRule /(.+)-99999x300.jpg /$1-169x300.jpg [QSA,L]

由于Modula Gallery创建了自己的缩略图,而LiteSpeed Cache的图像要小得多,所以我的目标是改为提供经过优化的图像。事实证明,根据图像的原始大小,我需要的URL将为300 x 169或300 x225。我最终检查以确保文件存在,并且在每个规则之前都存在重写条件: / p>

RewriteCond %{REQUEST_URI} ^/(.+)-300x99999.jpg$
RewriteCond %{DOCUMENT_ROOT}/$1-300x169.jpg -f [NC]
RewriteRule /(.+)-300x99999.jpg /$1-300x169.jpg [QSA,L]

RewriteCond %{REQUEST_URI} ^/(.+)-300x99999.jpg$
RewriteCond %{DOCUMENT_ROOT}/$1-300x225.jpg -f [NC]
RewriteRule /(.+)-300x99999.jpg /$1-300x225.jpg [QSA,L]

RewriteCond %{REQUEST_URI} ^/(.+)-99999x300.jpg$
RewriteCond %{DOCUMENT_ROOT}/$1-169x300.jpg -f [NC]
RewriteRule /(.+)-99999x300.jpg /$1-169x300.jpg [QSA,L]

RewriteCond %{REQUEST_URI} ^/(.+)-99999x300.jpg$
RewriteCond %{DOCUMENT_ROOT}/$1-225x300.jpg -f [NC]
RewriteRule /(.+)-99999x300.jpg /$1-225x300.jpg [QSA,L]