# ============================================================
# Laravel .htaccess
# Performance Optimization + Security + SSL + Caching
# ============================================================

# ------------------------------------------------------------
# 1. Disable directory listing and MultiViews
# ------------------------------------------------------------
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

# ------------------------------------------------------------
# 2. SSL FORCE (HTTP → HTTPS) - সবচেয়ে গুরুত্বপূর্ণ
# ------------------------------------------------------------
<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # HTTPS এ রিডাইরেক্ট করুন (যদি ইতিমধ্যে HTTPS না হয়)
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

# ------------------------------------------------------------
# 3. HSTS (HTTP Strict Transport Security) - 1 বছর
# ------------------------------------------------------------
<IfModule mod_headers.c>
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" env=HTTPS
</IfModule>

# ------------------------------------------------------------
# 4. Correct MIME types for modern assets
# ------------------------------------------------------------
<IfModule mime_module>
    AddType image/avif .avif
    AddType image/webp .webp
    AddType image/svg+xml .svg .svgz

    AddType font/woff .woff
    AddType font/woff2 .woff2
    AddType application/vnd.ms-fontobject .eot
    AddType font/ttf .ttf
    AddType font/otf .otf

    AddType application/javascript .js .mjs
    AddType application/wasm .wasm
</IfModule>

# ------------------------------------------------------------
# 5. Browser caching (Expires)
# ------------------------------------------------------------
<IfModule mod_expires.c>
    ExpiresActive On

    # Images - cache for 1 year
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/avif "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"

    # Fonts - cache for 1 year
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType font/ttf "access plus 1 year"
    ExpiresByType font/otf "access plus 1 year"
    ExpiresByType application/font-woff "access plus 1 year"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"

    # CSS and JavaScript - cache for 1 month
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"

    # Audio and video - cache for 1 month
    ExpiresByType audio/mpeg "access plus 1 month"
    ExpiresByType audio/ogg "access plus 1 month"
    ExpiresByType video/mp4 "access plus 1 month"
    ExpiresByType video/webm "access plus 1 month"
    ExpiresByType video/ogg "access plus 1 month"

    # Documents
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType application/wasm "access plus 1 year"
</IfModule>

# ------------------------------------------------------------
# 6. Brotli compression (when available)
# ------------------------------------------------------------
<IfModule mod_brotli.c>
    <IfModule mod_filter.c>
        AddOutputFilterByType BROTLI_COMPRESS \
            text/html \
            text/plain \
            text/css \
            text/xml \
            text/javascript \
            application/javascript \
            application/x-javascript \
            application/json \
            application/xml \
            application/xhtml+xml \
            application/rss+xml \
            application/atom+xml \
            image/svg+xml
    </IfModule>
</IfModule>

# ------------------------------------------------------------
# 7. Gzip fallback (when Brotli is unavailable)
# ------------------------------------------------------------
<IfModule !mod_brotli.c>
    <IfModule mod_deflate.c>
        <IfModule mod_filter.c>
            AddOutputFilterByType DEFLATE \
                text/html \
                text/plain \
                text/css \
                text/xml \
                text/javascript \
                application/javascript \
                application/x-javascript \
                application/json \
                application/xml \
                application/xhtml+xml \
                application/rss+xml \
                application/atom+xml \
                image/svg+xml
        </IfModule>
    </IfModule>
</IfModule>

# ------------------------------------------------------------
# 8. Do not compress already compressed files
# ------------------------------------------------------------
<IfModule mod_setenvif.c>
    SetEnvIfNoCase Request_URI \
        "\.(?:gif|jpe?g|png|webp|avif|zip|gz|rar|7z|mp4|webm|mp3|ogg|woff|woff2|pdf)$" \
        no-gzip dont-vary
</IfModule>

# ------------------------------------------------------------
# 9. Cache-Control & Security Headers
# ------------------------------------------------------------
<IfModule mod_headers.c>

    # Images and fonts
    <FilesMatch "\.(?:jpg|jpeg|png|gif|webp|avif|svg|ico|woff|woff2|ttf|otf|eot)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>

    # CSS and JavaScript
    <FilesMatch "\.(?:css|js|mjs)$">
        Header set Cache-Control "public, max-age=2592000"
    </FilesMatch>

    # Security Headers
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # Remove server info (optional)
    Header unset Server
    Header unset X-Powered-By
</IfModule>

# ------------------------------------------------------------
# 10. Laravel rewrite rules
# ------------------------------------------------------------
<IfModule mod_rewrite.c>

    RewriteEngine On

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

    # Handle X-XSRF-Token Header
    RewriteCond %{HTTP:x-xsrf-token} .
    RewriteRule .* - [E=HTTP_X_XSRF_TOKEN:%{HTTP:X-XSRF-Token}]

    # Redirect trailing slash if the request is not a folder
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send requests to Laravel front controller
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>

# ------------------------------------------------------------
# 11. PHP 8.2 cPanel handler
# ------------------------------------------------------------
# php -- BEGIN cPanel-generated handler, do not edit
<IfModule mime_module>
    AddHandler application/x-httpd-ea-php82 .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

# ------------------------------------------------------------
# 12. Block access to sensitive files
# ------------------------------------------------------------
<FilesMatch "^(\.env|\.git|composer\.json|composer\.lock|package\.json|package-lock\.json)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# ------------------------------------------------------------
# 13. Prevent access to the storage folder
# ------------------------------------------------------------
<IfModule mod_rewrite.c>
    RewriteRule ^storage/.*$ - [F,L]
</IfModule>