# =====================================================================
# Qendra e Dhëmbit Qeshu – static site (Next.js export) on cPanel/Apache
# =====================================================================

Options -Indexes -MultiViews
DirectoryIndex index.html index.php
AddDefaultCharset utf-8
AddType application/manifest+json .webmanifest
AddType image/avif .avif
AddType image/webp .webp

# Custom 404 (the English 404 is set in /en/.htaccess)
ErrorDocument 404 /404.html

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  # 1) Force HTTPS
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

  # 2) Canonical host without www
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

  # 3) Block secrets and hidden files (except .well-known for SSL validation)
  RewriteRule (^|/)config\.php$ - [F,L]
  RewriteRule (^|/)\.(?!well-known/) - [F,L]

  # 4) Albanian is served at the root: /sq/... -> /...
  RewriteRule ^sq(?:/(.*))?$ /$1 [L,R=301]

  # 5) Clean trailing-slash URLs: /path -> /path/ when /path/index.html exists
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !/$
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}/index.html -f
  RewriteRule ^ %{REQUEST_URI}/ [L,R=301]

  # 6) /path/ -> /path/index.html (explicit, in case DirectoryIndex is overridden)
  RewriteCond %{REQUEST_URI} /$
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}index.html -f
  RewriteRule ^(.*)$ $1index.html [L]
</IfModule>

# Compression: Brotli when available, otherwise gzip/deflate
<IfModule mod_brotli.c>
  AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml application/xml application/javascript text/javascript application/json application/ld+json application/manifest+json image/svg+xml
</IfModule>
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/xml application/javascript text/javascript application/json application/ld+json application/manifest+json image/svg+xml
</IfModule>

# Caching (hashed files under /_next/static/ and /images/ get 1 year via their own .htaccess)
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 hour"
  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType text/plain "access plus 0 seconds"
  ExpiresByType application/xml "access plus 1 hour"
  ExpiresByType text/xml "access plus 1 hour"
  ExpiresByType image/avif "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 month"
  ExpiresByType image/vnd.microsoft.icon "access plus 1 month"
  ExpiresByType font/woff2 "access plus 1 year"
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/javascript "access plus 1 year"
  ExpiresByType application/manifest+json "access plus 1 week"
</IfModule>

<IfModule mod_headers.c>
  # Short cache for HTML and the RSC payload files so new deployments show up immediately
  <FilesMatch "\.(html|txt)$">
    Header set Cache-Control "public, max-age=0, must-revalidate"
  </FilesMatch>

  # Security headers
  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"
  Header always set Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=(), magnetometer=(), gyroscope=(), accelerometer=()"
  Header always set Cross-Origin-Opener-Policy "same-origin"
  # Uncomment once HTTPS works on the subdomain and you are sure you will keep it:
  # Header always set Strict-Transport-Security "max-age=31536000"
  Header unset X-Powered-By
</IfModule>

# Deny direct access to config.php anywhere
<Files "config.php">
  Require all denied
</Files>
