RewriteEngine On
RewriteBase /

# 0. Защита на staging тестови скриптове и административни тестове (OWASP A05 / NIS2)
RewriteRule ^staging(/.*)?$ - [F,L]
RewriteRule ^tests(/.*)?$ - [F,L]
RewriteRule ^e2e_.*\.js$ - [F,L]

# Allow Lets Encrypt HTTP-01
RewriteRule ^\.well-known/acme-challenge/ - [L]

# 301 Redirect: /page.html -> /page
RewriteCond %{THE_REQUEST} \s/([^.]+)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L]

# Internal rewrite: /page -> page.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^/]+)/?$ $1.html [L]

<IfModule mod_headers.c>
	# HTML - кратък кеш с валидация за свежо съдържание
	<FilesMatch "\.html$">
		Header set Cache-Control "public, max-age=300, must-revalidate"
	</FilesMatch>

	# Robots, sitemap, llms.txt, llms-full.txt, pricing.md
	<FilesMatch "^(robots\.txt|sitemap\.xml|llms\.txt|llms-full\.txt|pricing\.md)$">
		Header set Cache-Control "public, max-age=3600, must-revalidate"
	</FilesMatch>

	# Статични ресурси: CSS, JS - кеширане за по-висок PageSpeed и Core Web Vitals
	<FilesMatch "\.(css|js)$">
		Header set Cache-Control "public, max-age=86400, must-revalidate"
	</FilesMatch>

	# Изображения и шрифтове - дълготраен кеш
	<FilesMatch "\.(jpg|jpeg|png|gif|svg|ico|webp|woff2|woff|ttf)$">
		Header set Cache-Control "public, max-age=2592000, immutable"
	</FilesMatch>
</IfModule>
