# ─── Production .htaccess for Apache/LiteSpeed/cPanel hosting ───
# Diving in Marsa Alam — bilingual EN/IT Vite SPA

# Enable rewrite engine
RewriteEngine On

# ─── 1. HTTP 301 Redirects for old Oceanic Whitetip URLs ───
# Old plural URL → new singular URL (English)
RewriteRule ^marine-life/oceanic-whitetip-sharks$ /marine-life/oceanic-whitetip-shark [R=301,L]

# Old plural URL → new singular URL (Italian)
RewriteRule ^it/marine-life/oceanic-whitetip-sharks$ /it/marine-life/oceanic-whitetip-shark [R=301,L]

# ─── 2. Serve prerendered static HTML where Available ───
# If a prerendered HTML file exists for the requested path, serve it
# instead of falling through to the SPA index.html.
# Prerendered files are in /prerendered/ directory (e.g. /prerendered/marine-life/dugong.html)
RewriteCond %{DOCUMENT_ROOT}/prerendered%{REQUEST_URI}.html -f
RewriteRule ^(.*)$ /prerendered$1.html [L]

# Handle the case where REQUEST_URI is a directory (e.g. /it/)
RewriteCond %{DOCUMENT_ROOT}/prerendered%{REQUEST_URI}index.html -f
RewriteRule ^(.*)$ /prerendered$1index.html [L]

# ─── 3. SPA Fallback ───
# All other routes fall back to the SPA index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

# ─── 4. Security Headers ───
<IfModule mod_headers.c>
  Header set X-Content-Type-Options "nosniff"
  Header set X-Frame-Options "SAMEORIGIN"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# ─── 5. Compression ───
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json image/svg+xml
</IfModule>

# ─── 6. Cache Static Assets ───
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/webp "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
  ExpiresByType text/html "access plus 1 hour"
</IfModule>
