# Apache Configuration for INGCO Website
# This file should be placed in the public/ or dist/ folder after build

# ============================================
# Force HTTPS Redirect
# ============================================
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} !^localhost [NC]
  RewriteCond %{HTTP_HOST} !^127\.0\.0\.1 [NC]
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# ============================================
# Cache Control Headers
# ============================================

<IfModule mod_headers.c>
  # HTML files - No cache (always fetch fresh)
  <FilesMatch "\.(html|htm)$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
  </FilesMatch>

  # JavaScript and CSS with hash in filename - Cache for 1 year (immutable)
  <FilesMatch "\.(js|css)$">
    # Check if filename contains hash (indicating it's a built file)
    <If "%{REQUEST_URI} =~ /-[a-f0-9]{8,}\.(js|css)$/">
      Header set Cache-Control "public, max-age=31536000, immutable"
    </If>
    # If no hash, don't cache
    <Else>
      Header set Cache-Control "no-cache, no-store, must-revalidate"
    </Else>
  </FilesMatch>

  # Images - Cache for 1 day
  <FilesMatch "\.(jpg|jpeg|png|gif|webp|svg|ico)$">
    Header set Cache-Control "public, max-age=86400"
  </FilesMatch>

  # Fonts - Cache for 1 year
  <FilesMatch "\.(woff|woff2|ttf|eot|otf)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>

  # JSON and other data files - No cache
  <FilesMatch "\.(json|xml)$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
  </FilesMatch>
</IfModule>

# ============================================
# Security Headers
# ============================================
<IfModule mod_headers.c>
  # Prevent MIME type sniffing
  Header set X-Content-Type-Options "nosniff"
  
  # Enable XSS protection
  Header set X-XSS-Protection "1; mode=block"
  
  # Prevent clickjacking
  Header set X-Frame-Options "SAMEORIGIN"
  
  # Referrer policy
  Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# ============================================
# SPA Routing (React Router)
# ============================================
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  
  # Don't rewrite files or directories that exist
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  
  # Rewrite everything else to index.html
  RewriteRule ^ index.html [L]
</IfModule>

# ============================================
# Compression (Gzip)
# ============================================
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json application/xml
</IfModule>

# ============================================
# ETag (Entity Tags) - Disable for better cache control
# ============================================
<IfModule mod_headers.c>
  Header unset ETag
</IfModule>
FileETag None

