# Kingdom Fight - Apache Configuration
# For deploying to Apache web servers

# Enable Rewrite Engine
<IfModule mod_rewrite.c>
    RewriteEngine On
    # Auto-detect base path (works for root and subdirectories)
    RewriteBase /
    
    # Force HTTPS (uncomment when you have SSL certificate)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # Remove www (optional - uncomment if you want to remove www)
    # RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    # RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    
    # API Routing - Route all /api/* requests to router.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/api/
    RewriteRule ^api/(.*)$ router.php [QSA,L]
    
    # Serve static files directly (CSS, JS, images, fonts)
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*)$ - [L]
    
    # Route frontend/index.html as default
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^$ frontend/index.html [L]
    
    # Handle 404 for non-existent files (except API)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/api/
    RewriteCond %{REQUEST_URI} !^/frontend/
    RewriteCond %{REQUEST_URI} !^/assets/
    RewriteRule ^(.*)$ frontend/404.html [L]
    
    # Route frontend files
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/frontend/
    RewriteRule ^frontend/(.*)$ frontend/$1 [L]
    
    # Route assets
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/assets/
    RewriteRule ^assets/(.*)$ assets/$1 [L]
</IfModule>

# Security Headers
<IfModule mod_headers.c>
    # Prevent clickjacking
    Header always set X-Frame-Options "SAMEORIGIN"
    
    # XSS Protection
    Header always set X-XSS-Protection "1; mode=block"
    
    # Prevent MIME type sniffing
    Header always set X-Content-Type-Options "nosniff"
    
    # Referrer Policy
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # Content Security Policy (adjust as needed)
    Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self';"
    
    # CORS Headers (for API - adjust allowed origins)
    <FilesMatch "\.(php)$">
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
        Header set Access-Control-Allow-Headers "Content-Type, Authorization"
        Header set Access-Control-Allow-Credentials "true"
    </FilesMatch>
</IfModule>

# PHP Configuration
<IfModule mod_php7.c>
    # Increase PHP limits for game operations
    php_value upload_max_filesize 10M
    php_value post_max_size 10M
    php_value max_execution_time 300
    php_value max_input_time 300
    php_value memory_limit 256M
    
    # Session configuration
    php_value session.cookie_httponly 1
    php_value session.use_only_cookies 1
    php_value session.cookie_secure 0
    php_value session.gc_maxlifetime 86400
</IfModule>

# Error Pages
ErrorDocument 404 /frontend/404.html
ErrorDocument 500 /frontend/500.html
ErrorDocument 403 /frontend/404.html

# Prevent directory listing
Options -Indexes

# Protect sensitive files
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|sql|bak|backup)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# Protect config files
<FilesMatch "^(config|database|\.env)">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# Cache Control for static assets
<IfModule mod_expires.c>
    ExpiresActive On
    
    # Images
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    
    # CSS and JavaScript
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    
    # Fonts
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType application/font-woff "access plus 1 year"
    ExpiresByType application/font-woff2 "access plus 1 year"
    
    # HTML (no cache for dynamic content)
    ExpiresByType text/html "access plus 0 seconds"
</IfModule>

# Compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# MIME Types
<IfModule mod_mime.c>
    AddType application/javascript js
    AddType text/css css
    AddType image/svg+xml svg
    AddType application/json json
</IfModule>

# Default charset
AddDefaultCharset UTF-8

# File encoding
<FilesMatch "\.(html|htm|css|js|json|xml)$">
    AddDefaultCharset UTF-8
</FilesMatch>
