# Apache .htaccess Configuration
# For React SPA routing and API endpoints

# Disable MultiViews to prevent 406 errors
Options -MultiViews -Indexes

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # API Routes - Only rewrite if not already in the endpoints folder
    RewriteCond %{REQUEST_URI} !^/api/endpoints/
    RewriteRule ^api/(.*)$ api/endpoints/$1 [L,QSA]

    # SEO Bridge for Blog Posts
    # This MUST run before the index.html fallback
    RewriteRule ^GuufsStories/.*$ seo.php [L,QSA]

    # Don't rewrite files or directories that exist
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # React Router - Send all other requests to index.html
    RewriteRule ^ index.html [L]
</IfModule>

# Security Headers
<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "DENY"
    Header set X-XSS-Protection "1; mode=block"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# Disable directory browsing
Options -Indexes

# Protect sensitive files
<FilesMatch "^\.env">
    Order allow,deny
    Deny from all
</FilesMatch>

# Enable GZIP compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
</IfModule>

# Browser caching
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/html "access plus 0 seconds"
</IfModule>
