RewriteEngine On
# 1) Canonical URLs: strip trailing slash (except bare root) — 301 for SEO.
RewriteCond %{REQUEST_URI} ^/.+/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
# 2) Serve pre-rendered static cache file directly, bypassing PHP entirely.
RewriteCond %{DOCUMENT_ROOT}/cache/$1/index.html -f
RewriteRule ^(.*)$ /cache/$1/index.html [L]
# 2b) Same, for a cached "typed text page" (/css/main.css, /robots.txt, …).
# It's stored as a flat file, not
/index.html, so Apache labels it
# with the real extension's MIME type instead of text/html. Extension
# list mirrors App\MediaType::MAP.
RewriteCond %{DOCUMENT_ROOT}/cache/$1 -f
RewriteRule ^(.+\.(?:txt|css|xml|json|js))$ /cache/$1 [L]
# 3) Serve real files/directories as-is (cache assets, favicon, etc.).
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# 4) Everything else goes through the front controller.
RewriteRule ^ index.php [L]