# Author: Eduard Laas
# 2005 - 2026 SLAED
# License: MIT
# Website: slaed.net

# Default charset
AddDefaultCharset utf-8

# Prevent directory listing
Options -Indexes

# Errors: app-servable statuses render the branded SLAED page via the front controller (whitelisted in core/security.php)
ErrorDocument 400 /index.php?error=400
ErrorDocument 401 /index.php?error=401
ErrorDocument 403 /index.php?error=403
ErrorDocument 404 /index.php?error=404
ErrorDocument 500 /index.php?error=500
ErrorDocument 503 /index.php?error=503

# Infrastructure statuses (PHP not running, e.g. Apache as a reverse proxy): serve the static self-contained page
ErrorDocument 502 /error.html
ErrorDocument 504 /error.html

# Deflate compression (Apache fallback; on nginx/LiteSpeed configure gzip/brotli in the server config)
# The filter is chosen by response type and not by file name, which is what covers the pages themselves: they are built by index.php and no file-name rule reaches them
# WOFF2/WOFF, images and archives are already compressed and are intentionally left out
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript text/xml text/xsl
AddOutputFilterByType DEFLATE application/javascript application/json application/xml application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml application/xslt+xml application/opensearchdescription+xml image/svg+xml
</IfModule>

# Browser cache lifetimes of the static files; pages and uploads are answered by PHP, which sends their own header
# A style or script whose address carries the version of its content (?v=) never changes under that address, so it is kept a year; any other static file a week, revalidated by ETag
# Without mod_headers no rule applies and the browser guesses a lifetime from the validators, as before
<IfModule mod_headers.c>
<FilesMatch "(?i)\.(?:css|js|mjs)$">
<If "%{QUERY_STRING} =~ /(?:^|&)v=[^&]/">
Header set Cache-Control "public, max-age=31536000, immutable"
</If>
<Else>
Header set Cache-Control "public, max-age=604800"
</Else>
</FilesMatch>
<FilesMatch "(?i)\.(?:woff2?|ttf|otf|eot|jpe?g|png|gif|webp|avif|svg|ico|mp3|ogg|wav)$">
Header set Cache-Control "public, max-age=604800"
</FilesMatch>
</IfModule>

# Mod rewrite on; no RewriteBase, so the rules hold with the root on public/ and with the root on the project, whose .htaccess rewrites into this folder
RewriteEngine On

# The theme markup and the PHP of a theme are assembled by PHP, no browser fetches them
RewriteRule ^templates/.*\.php$ - [F,L,NC]
RewriteRule ^templates/[^/]+/.*\.html$ - [F,L,NC]

# A dot file or folder is no address of the site; the ACME challenges of .well-known stay reachable
RewriteRule (?:^|/)\.(?!well-known/) - [F,L]

# Rewrite rules to block out some common exploits
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [NC,OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [NC,OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2}) [NC]
RewriteRule .* - [F,L]

# HTTP Authentication (must be BEFORE the front controller rule)
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirecting non-www to www
# RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
# RewriteCond %{HTTPS}s ^on(s)|
# RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# The requested path and file doesn't directly match a physical file or a physical folder internally, rewrite the request to the index.php
# No upload lies in this folder, so every address under uploads/ reaches index.php, whose light path serves a file of a public folder and refuses any other
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [QSA,L]
