# SPA redirect: serve existing files, otherwise fallback to index.html
Options -MultiViews
DirectoryIndex index.php index.html
ErrorDocument 404 /404.php
<IfModule mod_headers.c>
  <FilesMatch "^(sitemap\.php|llms\.php)$">
    Header set X-Robots-Tag "index, follow"
  </FilesMatch>
  Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" "expr=%{HTTPS} == 'on'"
  Header always set X-Content-Type-Options "nosniff"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  <FilesMatch "\.(?:js|css|woff2?|eot|ttf|otf)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
  <FilesMatch "\.(?:png|jpe?g|gif|svg|webp|avif|ico)$">
    Header set Cache-Control "public, max-age=31536000, immutable"
  </FilesMatch>
  <FilesMatch "^(?:robots|ads|llms)\.txt$">
    Header set Content-Type "text/plain; charset=utf-8"
    Header set Cache-Control "public, max-age=3600"
  </FilesMatch>
</IfModule>
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/javascript "access plus 1 year"
  ExpiresByType font/woff2 "access plus 1 year"
  ExpiresByType font/woff "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
  ExpiresByType image/webp "access plus 1 year"
  ExpiresByType image/avif "access plus 1 year"
</IfModule>
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  # Canonical host: www.pna.co.th → pna.co.th (matches PUBLIC_SITE_URL)
  RewriteCond %{HTTP_HOST} ^www\.pna\.co\.th$ [NC]
  RewriteRule ^(.*)$ https://pna.co.th/$1 [R=301,L]
  RewriteRule ^index\.html?$ / [R=301,L]
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} .+/$
  RewriteRule ^(.+)/$ /$1 [R=301,L]
  # Canonical: /services → /services/all (avoid duplicate listing pages)
  RewriteRule ^services/?$ /services/all [R=301,L]
  # Machine-readable discovery files for SEO/AEO.
  RewriteRule ^robots\.txt$ /robots.php [L]
  RewriteRule ^sitemap\.xml$ /sitemap.php [L]
  RewriteRule ^llms\.txt$ /llms.php [L]
  RewriteRule ^feed\.xml$ /feed.php [L]
  # If the request matches a real file or directory, serve it
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]
  # Otherwise, render the SPA through PHP so crawlers receive route-specific SEO.
  RewriteRule ^ /index.php [L]
</IfModule>
