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

    RewriteEngine On

    # ============================================
    # SECURITY: Block access to parent directory files
    # ============================================
    
    # Prevent access to .env from public folder
    RewriteRule ^(.*/)?\.env - [F,L]
    
    # Block access to sensitive files in parent directories
    RewriteRule ^(.*/)?\.git/ - [F,L]
    RewriteRule ^(.*/)?composer\.(json|lock) - [F,L]
    RewriteRule ^(.*/)?package.*\.json - [F,L]
    RewriteRule ^(.*/)?\.htaccess - [F,L]
    
    # Block direct access to storage folder
    RewriteRule ^storage/(.*)$ - [F,L]

    # ============================================
    # ROUTING
    # ============================================

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

# ============================================
# SECURITY HEADERS
# ============================================

# Prevent MIME-type sniffing
<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"
</IfModule>

# Protect sensitive files
<FilesMatch "^\.env">
    Order allow,deny
    Deny from all
</FilesMatch>
