.htaccess 从Apache Web服务器重写外部图像URL(Htaccess)

epfja78i  于 2023-03-23  发布在  Apache
关注(0)|答案(1)|浏览(98)

我需要重写所有的图像(除了webp)从我的服务器(Apache,WordPress的)的网址,但我的大部分图像是在CDN(digitalocean空间)
所以如果我的URL是https://halorumah.sgp1.digitaloceanspaces.com/wp-content/***<many subfolder>***/filename.png必须更改为https://proxied-url/converttowebp/https://halorumah.sgp1.digitaloceanspaces.com/wp-content/***<many subfolder>***/filename.png
我有这个重写规则对我的htaccess文件

<IfModule mod_rewrite.c>
 RewriteEngine On

 # Check if browser supports WebP
 RewriteCond %{HTTP_ACCEPT} image/webp
 # check if request is image jpg/jpeg/png
 RewriteCond %{REQUEST_FILENAME}.jpg -f [OR]
 RewriteCond %{REQUEST_FILENAME}.jpeg -f [OR]
 RewriteCond %{REQUEST_FILENAME}.png -f
 # rewrite url to proxied url
 RewriteRule ^(.*)$ https://proximg.halorumah.id/pximage/towebp/$1 [P]

</IfModule>

上面的重写规则不起作用。
条件是检查我的服务器是否可以提供webp版本的图像,我需要改变这个规则,所以即使图像是在外部服务器/cdn上,如果图像不是webp,我可以重写为代理url。

kse8i1jr

kse8i1jr1#

这条规则对你更好:

RewriteEngine On
RewriteRule ([^.]+\.(jpe?g|png))$ https://proxied-url/converttowebp/https://%{HTTP_HOST}/$1 [P]

相关问题