# Humanitree · .htaccess
# Works on Apache 2.4 (cPanel, LiteSpeed). Each block is wrapped in IfModule so a missing module never breaks the site.

# ---- basics ----
Options -Indexes
ServerSignature Off
DirectoryIndex index.html index.php
ErrorDocument 404 /404.html
ErrorDocument 403 /404.html
AddDefaultCharset utf-8

# ---- MIME types ----
<IfModule mod_mime.c>
  AddType font/woff2 .woff2
  AddType image/svg+xml .svg
  AddType application/manifest+json .webmanifest
  AddType text/plain .txt
</IfModule>

# ---- rewrites: HTTPS, non-www canonical, clean URLs ----
<IfModule mod_rewrite.c>
  RewriteEngine On

  # Block TRACE / TRACK
  RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
  RewriteRule .* - [F,L]

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

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

  # Trim /index.html to the folder URL
  RewriteCond %{THE_REQUEST} \s/+(.*/)?index\.html[\s?] [NC]
  RewriteRule ^(.*/)?index\.html$ /%1 [R=301,L,NE]

  # Add a trailing slash to directory URLs (keeps canonical URLs consistent)
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteCond %{REQUEST_URI} !/$
  RewriteRule ^(.*)$ /$1/ [R=301,L,NE]
</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=(), payment=(), usb=(), interest-cohort=()"
  Header always set Cross-Origin-Opener-Policy "same-origin"
  Header always set Cross-Origin-Resource-Policy "same-origin"
  Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'sha256-xMEne5xSNlgeOligOATNEvyFpyN5H3HG/E/qDyG8S5Y='; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; form-action 'self'; base-uri 'self'; object-src 'none'; upgrade-insecure-requests; frame-ancestors 'self'"
  # Enable HSTS once HTTPS is confirmed working on the domain (it is safe to leave on with a valid certificate)
  Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" "expr=%{HTTPS} == 'on'"
  Header unset X-Powered-By
  Header unset Server
  # Long cache for fingerprinted static assets, short for HTML
  <FilesMatch "\.(css|js|woff2|svg|png|jpg|ico|webmanifest)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
  <FilesMatch "\.(html|xml|txt)$">
    Header set Cache-Control "public, max-age=600, must-revalidate"
  </FilesMatch>
</IfModule>

# ---- compression ----
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json application/xml image/svg+xml application/manifest+json
</IfModule>
<IfModule mod_brotli.c>
  AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript application/json application/xml image/svg+xml
</IfModule>

# ---- expires (fallback for hosts without mod_headers) ----
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/x-icon "access plus 1 year"
  ExpiresByType text/html "access plus 10 minutes"
</IfModule>

# ---- lock down files that must never be served ----
<IfModule mod_authz_core.c>
  <FilesMatch "^(\.htaccess|\.htpasswd|\.env|\.git.*|.*\.(bak|old|orig|save|swp|sql|log|lock|ini|sh|md)|package(-lock)?\.json|README.*)$">
    Require all denied
  </FilesMatch>
</IfModule>
<IfModule !mod_authz_core.c>
  <FilesMatch "^(\.htaccess|\.htpasswd|\.env|\.git.*|.*\.(bak|old|orig|save|swp|sql|log|lock|ini|sh|md)|package(-lock)?\.json|README.*)$">
    Order allow,deny
    Deny from all
  </FilesMatch>
</IfModule>

# ---- PHP hardening (ignored on hosts that do not allow php_flag; harmless) ----
<IfModule mod_php.c>
  php_flag display_errors Off
  php_flag expose_php Off
  php_flag allow_url_fopen Off
  php_flag allow_url_include Off
</IfModule>
