# BiaRomania front-end rules (copied into the Document Root by the build).
# Needs mod_rewrite; the header rules need mod_headers (both are normal on cPanel).
Options -Indexes

<IfModule mod_rewrite.c>
  RewriteEngine On
  # /api is handled by api/.htaccess
  RewriteRule ^api/ - [L]
  # Real files and folders are served as they are
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]
  # Everything else is the single-page app
  RewriteRule ^ index.html [L]
</IfModule>

<IfModule mod_headers.c>
  Header always set X-Content-Type-Options "nosniff"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set X-Frame-Options "SAMEORIGIN"
  # microphone=(self): administrators record pronunciation in the browser (admin panel); no other site may use it
  Header always set Permissions-Policy "camera=(), microphone=(self), geolocation=()"
  # Content-Security-Policy: only this site's own scripts, styles, images and API. No inline scripts, no plugins,
  # no framing by other sites. 'unsafe-inline' for styles only (React style props and the theme accent).
  Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self'; connect-src 'self'; worker-src 'self'; manifest-src 'self'; media-src 'self' blob:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'"
  Header always set Cross-Origin-Opener-Policy "same-origin"
  # HSTS only when the request really arrived over HTTPS (never on a plain-http test install).
  Header always set Strict-Transport-Security "max-age=15552000" "expr=%{HTTPS} == 'on' || req('X-Forwarded-Proto') == 'https'"
  # Always re-check these; hashed files in /assets never change
  <FilesMatch "^(index\.html|sw\.js|boot\.js|manifest\.webmanifest)$">
    Header set Cache-Control "no-cache"
  </FilesMatch>
  <If "%{REQUEST_URI} =~ m#^/assets/#">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </If>
  # Pronunciation recordings: a file never changes (a new voice gets a new /audio/ro-vN/ folder); the list does.
  <If "%{REQUEST_URI} =~ m#^/audio/.+\.mp3$#">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </If>
  <If "%{REQUEST_URI} =~ m#^/audio/.+/manifest\.json$#">
    Header set Cache-Control "no-cache"
  </If>
</IfModule>

# Never serve hidden files
<FilesMatch "^\.">
  Require all denied
</FilesMatch>
