# Image optimization rules
# Enable compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# Browser caching
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
</IfModule>

# Cache control headers
<IfModule mod_headers.c>
    <FilesMatch "\.(jpg|jpeg|png|gif|webp|svg)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
        Header set X-Content-Type-Options nosniff
    </FilesMatch>
</IfModule>

# WebP serving for supporting browsers
<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Check if browser supports WebP
    RewriteCond %{HTTP_ACCEPT} image/webp
    # Check if WebP version exists
    RewriteCond %{REQUEST_FILENAME}.webp -f
    # Serve WebP version
    RewriteRule (.+)\.(jpe?g|png)$ $1.$2.webp [T=image/webp,E=accept:1,L]
</IfModule>

# Add WebP MIME type
<IfModule mod_mime.c>
    AddType image/webp .webp
</IfModule>