# Apache configuration for IBDL LMS Frontend
# This file goes in: public/ibdlportal.invade.solutions/

# Enable rewrite engine
RewriteEngine On

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

# Proxy API requests to backend Node.js server
# Change port 5000 to whatever port your backend runs on
RewriteCond %{REQUEST_URI} ^/api/
RewriteRule ^api/(.*)$ http://127.0.0.1:5000/api/$1 [P,L]

# Proxy WebSocket connections (for real-time features)
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule ^(.*)$ ws://127.0.0.1:5000/$1 [P,L]

# Enable proxy for API requests
<IfModule mod_proxy.c>
  ProxyPreserveHost On
  ProxyRequests Off
</IfModule>

# Serve static files directly (images, CSS, JS, etc.)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# SPA Routing: Redirect all other requests to index.html
# This allows React Router to handle client-side routing
RewriteRule ^ /index.html [L]

# Security headers
<IfModule mod_headers.c>
  # Prevent clickjacking
  Header always set X-Frame-Options "SAMEORIGIN"

  # XSS Protection
  Header always set X-XSS-Protection "1; mode=block"

  # Prevent MIME sniffing
  Header always set X-Content-Type-Options "nosniff"

  # Referrer Policy
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# Compression for text-based resources
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

# Browser caching for static assets
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpg "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/pdf "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 year"
</IfModule>
