# =====================================================================
# public/.htaccess  -  Apache rules for the live cPanel server.
# (The local test server "php -S" ignores this file.)
# =====================================================================

# No folder listings, and index.php is the home page.
Options -Indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Force HTTPS. (.well-known is left alone so AutoSSL can renew the certificate.)
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP:X-Forwarded-Proto} !=https
    RewriteCond %{REQUEST_URI} !^/\.well-known/
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Block hidden files such as .git or .env (but not .well-known).
    RewriteRule (^|/)\.(?!well-known/) - [F]
</IfModule>

<IfModule mod_headers.c>
    # HSTS: browsers use HTTPS only for the next year.
    # (PHP pages send this too; this line covers CSS, JS and icons.)
    Header always set Strict-Transport-Security "max-age=31536000" env=HTTPS
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "DENY"
    Header always set Referrer-Policy "same-origin"
</IfModule>

# cPanel's PHP writes errors to a file called "error_log" next to the script,
# which here would be inside the web root. Never let a browser read it.
<FilesMatch "^(error_log|php\.ini|.*\.(log|sql|gz|bak))$">
    Require all denied
</FilesMatch>

# ---------- Speed: compress text files, cache files for a long time ----------
# app.css / app.js / parser.js are linked with ?v=<file date>, so a changed
# file always gets a new address and phones never keep an old copy.

<IfModule mod_mime.c>
    AddType application/manifest+json .webmanifest
</IfModule>

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/javascript text/javascript application/json application/manifest+json text/csv
</IfModule>

<IfModule mod_headers.c>
    <FilesMatch "\.(css|js|png|svg|ico)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
    # The service worker and manifest must always be fresh.
    <FilesMatch "^(sw\.js|manifest\.webmanifest)$">
        Header set Cache-Control "no-cache"
    </FilesMatch>
</IfModule>
