# Enable rewrite engine
RewriteEngine On

# Redirect all traffic to HTTPS in production (uncomment if needed)
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# React Router - Redirect all requests to index.html
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]

# Prevent direct access to .env files
<Files .env*>
    Require all denied
</Files>

# Security headers (if mod_headers is enabled)
<IfModule mod_headers.c>
    # Cache static assets for 1 year
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|svg|woff|woff2|ttf|otf)$">
        Header set Cache-Control "max-age=31536000, public, immutable"
    </FilesMatch>
    
    # Cache HTML files for 10 minutes
    <FilesMatch "\.(html|htm)$">
        Header set Cache-Control "max-age=600, must-revalidate"
    </FilesMatch>
</IfModule>

# Compression (if mod_deflate is enabled)
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# Error pages (customize as needed)
# ErrorDocument 404 /index.html
# ErrorDocument 500 /index.html