# For full documentation and other suggested options, please see
# http://rtfm.modx.com/evolution/1.0/administration/friendly-url-solutions
# including for unexpected logouts in multi-server/cloud environments
# and especially for the first three commented out rules

#php_flag register_globals Off
AddDefaultCharset utf-8
#php_value date.timezone Europe/Kyiv

#Options +FollowSymlinks
RewriteEngine On

# Prevent directory listing
Options -Indexes

# Allow .well-known directory (for SSL, domain verification, etc.) blocking other dotfiles and dotfolders at any level
RewriteRule ^\.well-known/ - [L]
RewriteCond %{REQUEST_URI} (^|/)\. [NC]
RewriteRule .* - [F,L]

RewriteBase /

# Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]

# Видаляємо повторні slash у URL.
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=301,L]

#redirect http to https
#RewriteCond %{HTTP:SSL} !=1 [NC]
#RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [L,R=301]
#or
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI}

#RewriteCond %{HTTPS} !=on
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#work for litespeed
RewriteCond %{HTTPS}  !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]


# Rewrite domain.com -> www.domain.com -- used with SEO Strict URLs plugin
#RewriteCond %{HTTP_HOST} !^$
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteCond %{HTTP_HOST} (.+)$
#RewriteRule ^(.*)$ http://www.%1/$1 [R=permanent,L] .

# without www
#RewriteCond %{HTTP_HOST} .
#RewriteCond %{HTTP_HOST} !^example\.com [NC]
#RewriteRule (.*) http://example.com/$1 [R=301,L]

# without www all domains
#RewriteCond %{HTTP_HOST} .
#RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


# 301 that removes index.php?q= from all urls
#RewriteCond %{THE_REQUEST} \s/+index\.php\?&q=([^\s&]+) [NC]
#RewriteRule ^ /%1? [R=301,L]
#RewriteCond %{THE_REQUEST} \s/+index\.php\?q=([^\s&]+) [NC]
#RewriteRule ^ /%1? [R=301,L]

# Exclude /assets and /manager directories and images from rewrite rules
RewriteRule ^(manager|assets|js|css|images|img)/.*$ - [L]
RewriteRule \.(jpg|jpeg|png|gif|ico|svg|webp|css|js|woff|woff2|ttf)$ - [L]

# For Friendly URLs

# Мовні префікси: /ru/... та /en/...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(ru|en)/?$ index.php?q=&lang=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(ru|en)/(.*)$ index.php?q=$2&lang=$1 [L,QSA]

# Стандартні Friendly URLs Evolution CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


# Reduce server overhead by enabling output compression if supported.
#php_flag zlib.output_compression On
#php_value zlib.output_compression_level 5


<IfModule mod_expires.c>
    ExpiresActive On

    # За замовчуванням — кешування на 1 місяць
    ExpiresDefault "access plus 1 month"

    # HTML та дані не кешуємо надовго
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType text/xml "access plus 0 seconds"
    ExpiresByType application/xml "access plus 0 seconds"
    ExpiresByType application/json "access plus 0 seconds"

    # RSS
    ExpiresByType application/rss+xml "access plus 1 hour"

    # Зображення
    ExpiresByType image/x-icon "access plus 1 week"
    ExpiresByType image/gif "access plus 6 months"
    ExpiresByType image/png "access plus 6 months"
    ExpiresByType image/jpeg "access plus 6 months"
    ExpiresByType image/webp "access plus 6 months"
    ExpiresByType image/avif "access plus 6 months"
    ExpiresByType image/svg+xml "access plus 6 months"

    # Відео та аудіо
    ExpiresByType video/ogg "access plus 6 months"
    ExpiresByType video/mp4 "access plus 6 months"
    ExpiresByType video/webm "access plus 6 months"
    ExpiresByType audio/ogg "access plus 6 months"

    # Шрифти
    ExpiresByType font/truetype "access plus 6 months"
    ExpiresByType font/opentype "access plus 6 months"
    ExpiresByType font/woff "access plus 6 months"
    ExpiresByType font/woff2 "access plus 6 months"
    ExpiresByType application/vnd.ms-fontobject "access plus 6 months"

    # CSS та JavaScript
    ExpiresByType text/css "access plus 6 months"
    ExpiresByType application/javascript "access plus 6 months"
    ExpiresByType text/javascript "access plus 6 months"
</IfModule>