#############################################
# .htaccess for Vite / React single-page app
# Place this file in the folder you deploy (commonly the build output / `dist` directory)
# Requirements on server: Apache with `mod_rewrite`, `mod_headers`, and `mod_expires` enabled.
#############################################

# Disable MultiViews to avoid unexpected content negotiation
Options -MultiViews

RewriteEngine On

# If your app is deployed to a subdirectory, set the correct base here (example: /myapp/)
# RewriteBase /

# Serve pre-compressed files when available (Brotli or gzip)
# Requires mod_rewrite + mod_headers and that precompressed files (.br/.gz) are generated
# Example for `.js`, `.css`, `.svg`, `.html` etc. Uncomment if you generate .br/.gz files.
#
#RewriteCond %{HTTP:Accept-encoding} br
#RewriteCond %{REQUEST_FILENAME}.br -f
#RewriteRule ^(.*)$ $1.br [L]
#Header set Content-Encoding br
#Header set Content-Type "text/javascript" env=IS_JS

# Prevent access to sensitive files
<FilesMatch "^(?:\.env|\.git|composer\.json|composer\.lock|README|LICENSE)$">
  Require all denied
</FilesMatch>

# Deny direct access to dotfiles
<FilesMatch "^\.">
  Require all denied
</FilesMatch>

# Set caching for static assets
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 month"
  ExpiresByType text/html "access plus 0 seconds"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/x-javascript "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType font/woff2 "access plus 1 month"
  ExpiresByType font/woff "access plus 1 month"
  ExpiresByType application/font-woff2 "access plus 1 month"
  ExpiresByType application/font-woff "access plus 1 month"
</IfModule>

# Ensure index.html is not aggressively cached (so clients pick up new versions)
<IfModule mod_headers.c>
  <FilesMatch "index\.html$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
  </FilesMatch>

  # Common security headers
  Header always set X-Content-Type-Options "nosniff"
  Header always set X-Frame-Options "DENY"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set X-XSS-Protection "1; mode=block"
</IfModule>

# Redirect all non-file, non-directory requests to index.html
# This enables client-side routing for a SPA (React + Vite)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, serve index.html
RewriteRule ^ index.html [L]

# Optionally: force HTTPS (uncomment if you want Apache to redirect HTTP -> HTTPS)
#RewriteCond %{HTTPS} !=on
#RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#############################################
#+ Notes:
#+ - Put this `.htaccess` in the directory you are serving (commonly `dist/` after `npm run build`).
#+ - If deploying under a subpath, set `RewriteBase /subpath/` accordingly.
#+ - If you want to serve pre-compressed assets, build them and enable the related blocks above.
#+ - Ensure Apache modules `mod_rewrite`, `mod_expires`, and `mod_headers` are enabled.
#############################################
