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

# ==============================================================================
# 1. SECURITY HEADERS
# ==============================================================================
<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
    Header unset X-Powered-By
</IfModule>

# ==============================================================================
# 2. BLOCK SENSITIVE FILES AND EXTENSIONS
# ==============================================================================
<FilesMatch "(?i)(\.env|\.git|\.htaccess|\.htpasswd|composer\.(json|lock)|package(-lock)?\.json|\.(sql|bak|backup|log|sh|ini|conf))$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order Deny,Allow
        Deny from all
    </IfModule>
</FilesMatch>

# ==============================================================================
# 3. REWRITE ENGINE & DIRECTORY RESTRICTIONS
# ==============================================================================
<IfModule mod_rewrite.c>
    RewriteEngine On

    # Block direct HTTP access to life internal directory
    RewriteRule ^life(/.*)?$ - [F,L,NC]

    # Block attempts to view dotfiles (.env, .git, etc.)
    RewriteRule (^|/)\.(?!well-known/) - [F,L]

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

    # 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>
