Во-первых, изображения находятся во многих подпапках. например: public_html/wp-content/uploads/2023/10, 2024/02, 2024/05 вот так.
Во-вторых, в каждой папке у меня есть формат image.jpg и формат image.jpg.webp. сервер не выбирает image.jpg.webp. Я хочу использовать этот формат.
В-третьих, мой .htaccess находится в папке public_html.
Вот коды, которые я пробовал:
Код: Выделить всё
RewriteEngine On
# Check if browser supports WebP
RewriteCond %{HTTP_ACCEPT} image/webp
# Check if WebP version of the image exists
RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
# Serve the WebP version if available
RewriteRule (.+)\.(jpg|jpeg|png|gif)$ $1.webp [T=image/webp,E=Cache-Control:vary=webp]
# Add the Vary header to inform browsers and proxies about WebP support
Header append Vary Accept
AddType image/webp .webp
Код: Выделить всё
RewriteCond %{REQUEST_FILENAME} (.+)\.(jpe?g|png|gif)$
RewriteCond %1\.webp -f
RewriteCond %{QUERY_STRING} !type=original
RewriteRule (.+)\.(jpe?g|png|gif)$ $1.webp [NC,T=image/webp,E=webp,L]
Код: Выделить всё
# Check if the requested file is a supported image format
RewriteCond %{REQUEST_FILENAME} \.(jpg|jpeg|png|gif)$ [NC]
# Check if a corresponding WebP version exists in the same directory
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.webp -f
# Serve the .webp version instead
RewriteRule (.+)\.(jpg|jpeg|png|gif)$ $1.$2.webp [T=image/webp,E=accept:1,L]
Код: Выделить всё
# Only target images within the wp-content/uploads folder (and its subfolders)
RewriteCond %{REQUEST_URI} ^/wp-content/uploads/ [NC]
# Check if a WebP version of the file exists (recursively in subfolders)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.webp -f
# Serve the .webp version if it exists
RewriteRule (.+)\.(jpg|jpeg|png|gif)$ $1.$2.webp [T=image/webp,E=accept:1,L]
Код: Выделить всё
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME}.webp -f
RewriteRule ^/?(.+?)\.(jpe?g|png)$ /$1.$2.webp [NC,T=image/webp,E=EXISTING:1,E=ADDVARY:1,L]
Код: Выделить всё
RewriteCond %{REQUEST_URI} ^/uploads/2023/10/(.+)(?:\.jpe?g|\.png)$
RewriteCond %{DOCUMENT_ROOT}/uploads/2023/10/%1.webp -f
RewriteRule ^/uploads/2023/10/%1.webp [L,T=image/webp,E=accept:1]
Подробнее здесь: https://stackoverflow.com/questions/791 ... h-htaccess