# ============================================================
#  Ali & Hala — Wedding Invitation (static export)
#  Drop this file into the same folder as index.html (public_html/).
# ============================================================

# Make sure Apache picks up index.html in every directory.
DirectoryIndex index.html

# Default charset.
AddDefaultCharset UTF-8

# Pretty error page.
ErrorDocument 404 /404.html

# ------------------------------------------------------------
# Rewrites
# ------------------------------------------------------------
<IfModule mod_rewrite.c>
  RewriteEngine On

  # Force HTTPS (uncomment after the SSL certificate is active).
  # RewriteCond %{HTTPS} !=on
  # RewriteCond %{HTTP_HOST} !^localhost [NC]
  # RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # Strip "index.html" from the URL (clean URLs).
  RewriteCond %{THE_REQUEST} \s/+(.*?)/?index\.html[\s?] [NC]
  RewriteRule ^ /%1 [R=301,L,NE]

  # If a real file or directory exists, serve it as-is.
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]

  # Map /path → /path/index.html (Next.js trailingSlash export style).
  RewriteCond %{REQUEST_FILENAME}/index.html -f
  RewriteRule ^(.+?)/?$ /$1/index.html [L]
</IfModule>

# ------------------------------------------------------------
# Compression (Gzip / Brotli)
# ------------------------------------------------------------
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
  AddOutputFilterByType DEFLATE application/javascript application/x-javascript
  AddOutputFilterByType DEFLATE application/json application/xml application/rss+xml
  AddOutputFilterByType DEFLATE image/svg+xml image/x-icon
  AddOutputFilterByType DEFLATE font/ttf font/otf font/woff font/woff2 application/font-woff
</IfModule>

<IfModule mod_brotli.c>
  AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml
  AddOutputFilterByType BROTLI_COMPRESS application/javascript application/json
  AddOutputFilterByType BROTLI_COMPRESS image/svg+xml font/woff font/woff2
</IfModule>

# ------------------------------------------------------------
# Cache headers
# ------------------------------------------------------------
<IfModule mod_expires.c>
  ExpiresActive On

  # HTML — short cache, must revalidate.
  ExpiresByType text/html                       "access plus 0 seconds"

  # Next.js immutable assets (hashed file names).
  <FilesMatch "\.(js|css)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>

  # Fonts.
  ExpiresByType font/woff                       "access plus 1 year"
  ExpiresByType font/woff2                      "access plus 1 year"
  ExpiresByType font/ttf                        "access plus 1 year"
  ExpiresByType font/otf                        "access plus 1 year"
  ExpiresByType application/font-woff           "access plus 1 year"
  ExpiresByType application/font-woff2          "access plus 1 year"

  # Images.
  ExpiresByType image/jpeg                      "access plus 6 months"
  ExpiresByType image/png                       "access plus 6 months"
  ExpiresByType image/webp                      "access plus 6 months"
  ExpiresByType image/avif                      "access plus 6 months"
  ExpiresByType image/svg+xml                   "access plus 6 months"
  ExpiresByType image/x-icon                    "access plus 1 year"

  # Audio / Video.
  ExpiresByType audio/mpeg                      "access plus 6 months"
  ExpiresByType audio/mp3                       "access plus 6 months"
  ExpiresByType video/mp4                       "access plus 6 months"
  ExpiresByType video/webm                      "access plus 6 months"

  # JS / CSS / JSON fallback (already overridden for /_next/ via FilesMatch above).
  ExpiresByType application/javascript          "access plus 1 month"
  ExpiresByType application/json                "access plus 0 seconds"
  ExpiresByType text/css                        "access plus 1 month"
</IfModule>

<IfModule mod_headers.c>
  # Long-cache the Next.js immutable chunk folder.
  <FilesMatch "^.*\.(js|css|woff2?|ttf|otf|eot|svg|jpe?g|png|webp|avif|gif|ico|mp4|webm|mp3)$">
    Header append Vary Accept-Encoding
  </FilesMatch>

  # No cache for HTML so updates show up immediately.
  <FilesMatch "\.html$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
  </FilesMatch>
</IfModule>

# ------------------------------------------------------------
# Correct MIME types (only if your host hasn't set them globally)
# ------------------------------------------------------------
<IfModule mod_mime.c>
  AddType application/javascript        .js
  AddType text/css                      .css
  AddType image/svg+xml                 .svg
  AddType image/webp                    .webp
  AddType image/avif                    .avif
  AddType font/woff                     .woff
  AddType font/woff2                    .woff2
  AddType audio/mpeg                    .mp3
  AddType video/mp4                     .mp4
</IfModule>

# ------------------------------------------------------------
# Security headers
# ------------------------------------------------------------
<IfModule mod_headers.c>
  Header always set X-Content-Type-Options "nosniff"
  Header always set X-Frame-Options "SAMEORIGIN"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"

  # Remove server signature.
  Header unset X-Powered-By
  Header always unset X-Powered-By
</IfModule>

ServerSignature Off

# Block access to dotfiles (.env, .git, etc.) except .well-known.
<FilesMatch "^\.(?!well-known)">
  Require all denied
</FilesMatch>
