# ==============================================================================
# BuilderDesk CRM - Apache .htaccess Configuration
# Tailored for GoDaddy Linux Shared Hosting (cPanel Apache 2.4+)
# ==============================================================================

Options -Indexes
ServerSignature Off

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # 1. Enforce HTTPS in Production (Uncomment on live GoDaddy domain with SSL)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # 2. Block direct access to hidden files, sensitive configurations, and databases
    <FilesMatch "(^\.env|^\.git|composer\.(json|lock)|\.sql|\.log|database\.php)$">
        Require all denied
    </FilesMatch>

    # 3. Block directory browsing or sensitive code execution in backend internal folders
    RewriteRule ^backend/config/ - [F,L]
    RewriteRule ^database/ - [F,L]
    RewriteRule ^backend/middleware/ - [F,L]

    # 4. In uploads directory, forbid execution of any PHP scripts (prevent web shells)
    <Directory "uploads">
        <FilesMatch "\.(php|phtml|php3|php4|php5|php7|phps)$">
            Require all denied
        </FilesMatch>
    </Directory>

    # 5. Route Extensionless API Requests to PHP endpoints
    # e.g., /api/auth/login -> /api/auth/login.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^api/(.*)$ api/$1.php [L,QSA]

    # 6. React SPA Client-side Routing Fallback
    # If the requested path is not an existing file or directory, and not an /api/ path, route to index.html
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/api/
    RewriteRule ^ index.html [L]
</IfModule>

# ==============================================================================
# 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"
</IfModule>

# ==============================================================================
# Gzip Compression for Fast Assets Loading
# ==============================================================================
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css application/xml application/xhtml+xml application/rss+xml application/javascript application/x-javascript application/json
</IfModule>

# ==============================================================================
# Browser Caching for Production Performance
# ==============================================================================
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>
