# =====================================================================
# Uxendi / Ushindi IPTV Platform — cPanel Apache / LiteSpeed Config
# Upload all files in this package directly into public_html/
# =====================================================================

Options -Indexes +FollowSymLinks
DirectoryIndex index.html

# 1. Proper MIME Types for HLS, MPEG-TS, CMAF & PWA
<IfModule mod_mime.c>
  AddType application/vnd.apple.mpegurl .m3u .m3u8
  AddType video/mp2t .ts
  AddType video/iso.segment .m4s
  AddType application/dash+xml .mpd
  AddType application/manifest+json .webmanifest
</IfModule>

# 2. CORS Headers for Media & Manifests
<IfModule mod_headers.c>
  Header set X-Content-Type-Options "nosniff"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
  <FilesMatch "\.(m3u|m3u8|ts|m4s|mp4|mpd|webmanifest)$">
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, HEAD, OPTIONS"
    Header set Access-Control-Allow-Headers "Content-Type, Range"
    Header set Access-Control-Expose-Headers "Content-Length, Content-Range, Content-Type"
  </FilesMatch>
</IfModule>

# 3. GZIP / Deflate Compression for Fast Loading
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json application/vnd.apple.mpegurl application/manifest+json
</IfModule>

# 4. URL Rewriting: HTTPS Redirect + API & Stream Proxy Routing
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # Block direct web access to private SQLite database folder
  RewriteRule ^data(/.*)?$ - [F,L]

  # Force HTTPS in production (skip on localhost)
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
  RewriteCond %{HTTP_HOST} !^(localhost|127\.0\.0\.1) [NC]
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # Route Stream Proxy (/api/v1/proxy) -> api/proxy.php
  RewriteRule ^api/v1/proxy/?$ api/proxy.php [QSA,L]

  # Route Backend REST API (/api/v1/*) -> api/index.php
  RewriteRule ^api/v1/(.*)$ api/index.php?route=$1 [QSA,L]

  # Serve existing static files and directories directly
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]

  # SPA Fallback -> index.html
  RewriteRule ^ index.html [L]
</IfModule>
