# Force HTTPS (comment for local testing, uncomment for production)
# RewriteEngine On
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Security Headers
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-XSS-Protection "1; mode=block"

# Block common hacking tools
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (nikto|wget|curl|libwww-perl|sqlmap|nmap|masscan|zgrab) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^-?$ [NC]
RewriteRule ^.*$ - [F,L]

# Prevent directory listing
Options -Indexes

# PROTECT SENSITIVE FILES - Use FilesMatch for specific files
<FilesMatch "^(config|exfil|token_functions)\.php$">
    Order Deny,Allow
    Deny from all
    # Allow from localhost for development
    Allow from 127.0.0.1
    Allow from ::1
</FilesMatch>

# Protect log files
<FilesMatch "\.(log|txt|bak|old)$">
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    Allow from ::1
</FilesMatch>

# DO NOT BLOCK THE SECURITY FOLDER - Allow access to PHP files in security folder
<Files "security/*.php">
    Order Allow,Deny
    Allow from all
</Files>

# Allow access to all other PHP files
<Files "*.php">
    Order Allow,Deny
    Allow from all
</Files>

# Compression for better performance
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>

# Cache static assets
<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 text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>

# Custom error pages
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
ErrorDocument 500 /500.html