<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # ------------------------------------------------------------------
    # Канонизация домена: единый хост (без www) и только HTTPS.
    # Устраняет дубли www/non-www и http/https (301-редиректы).
    # Условие X-Forwarded-Proto — чтобы не зациклиться за прокси/CDN.
    # ------------------------------------------------------------------
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP:X-Forwarded-Proto} !=https
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

    # ------------------------------------------------------------------
    # Прозрачная отдача WebP: если браузер поддерживает image/webp и рядом
    # с исходным png/jpg есть .webp — отдаём его вместо оригинала.
    # ------------------------------------------------------------------
    RewriteCond %{HTTP_ACCEPT} image/webp
    RewriteCond %{REQUEST_FILENAME} (?i)^(.+)\.(png|jpe?g)$
    RewriteCond %1.webp -f
    RewriteRule (?i)^(.+)\.(png|jpe?g)$ $1.webp [T=image/webp,L]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

# ------------------------------------------------------------------
# Сжатие текстовых ресурсов (gzip)
# ------------------------------------------------------------------
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
    AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript application/x-javascript text/javascript
    AddOutputFilterByType DEFLATE application/json application/ld+json
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE font/ttf font/otf font/woff font/woff2 application/vnd.ms-fontobject
</IfModule>

# ------------------------------------------------------------------
# Кэширование статики в браузере
# ------------------------------------------------------------------
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault                          "access plus 1 month"
    ExpiresByType image/webp                "access plus 1 year"
    ExpiresByType image/png                 "access plus 1 year"
    ExpiresByType image/jpeg                "access plus 1 year"
    ExpiresByType image/gif                 "access plus 1 year"
    ExpiresByType image/svg+xml             "access plus 1 year"
    ExpiresByType image/x-icon              "access plus 1 year"
    ExpiresByType font/woff2                "access plus 1 year"
    ExpiresByType font/woff                 "access plus 1 year"
    ExpiresByType font/ttf                  "access plus 1 year"
    ExpiresByType text/css                  "access plus 1 year"
    ExpiresByType application/javascript    "access plus 1 month"
    ExpiresByType text/javascript           "access plus 1 month"
    ExpiresByType text/html                 "access plus 0 seconds"
</IfModule>

<IfModule mod_headers.c>
    # Vary: Accept — чтобы прокси не кэшировали webp/png под одним ключом
    <FilesMatch "\.(png|jpe?g)$">
        Header append Vary Accept
    </FilesMatch>
    # Долгий кэш неизменяемой статики (CSS версионируется через ?v=)
    <FilesMatch "\.(webp|png|jpe?g|gif|svg|ico|css|woff2?|ttf|otf|eot)$">
        Header set Cache-Control "public, max-age=31536000"
    </FilesMatch>
    <FilesMatch "\.js$">
        Header set Cache-Control "public, max-age=2592000"
    </FilesMatch>
</IfModule>
