# ================================================================================
# Security Headers Configuration for Apache
# ================================================================================
# Add these lines to your .htaccess file to enhance security
# Make sure mod_headers is enabled: sudo a2enmod headers && systemctl restart apache2
# ================================================================================
# ============== Security Headers ==============
# Prevent clickjacking attacks
Header always set X-Frame-Options "DENY"
# Prevent MIME type sniffing
Header always set X-Content-Type-Options "nosniff"
# Enable XSS protection
Header always set X-XSS-Protection "1; mode=block"
# Enforce HTTPS (uncomment if you have SSL)
# Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
# Content Security Policy
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.google.com https://www.gstatic.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:; connect-src 'self';"
# Referrer Policy
Header always set Referrer-Policy "strict-origin-when-cross-origin"
# Permissions Policy
Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
# Cross-Domain Policy
Header always set X-Permitted-Cross-Domain-Policies "none"
# Remove server version information
Header unset X-Powered-By
Header always unset X-Powered-By
# ============== Hide PHP Version ==============
php_flag expose_php off
# ============== Disable Directory Browsing ==============
Options -Indexes
# ============== Protect Sensitive Files ==============
Order allow,deny
Deny from all
# Protect .env file
Order allow,deny
Deny from all
# Protect composer files
Order allow,deny
Deny from all
# Protect git files
Order allow,deny
Deny from all
# ============== SQL Injection Protection ==============
RewriteEngine On
# Block suspicious query strings
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} (\.\./|\.\.) [OR]
RewriteCond %{QUERY_STRING} (union|select|insert|drop|update|delete|exec|script|javascript|eval) [NC]
RewriteRule ^(.*)$ - [F,L]
# Block SQL injection attempts in URL
RewriteCond %{REQUEST_URI} (;|<|>|'|"|\)|%0A|%0D|%27|%3C|%3E|%00) [NC,OR]
RewriteCond %{REQUEST_URI} (boot\.ini|etc/passwd|self/environ) [NC,OR]
RewriteCond %{REQUEST_URI} (thumbs\.db|Thumbs\.db|\.DS_Store) [NC]
RewriteRule ^(.*)$ - [F,L]
# ============== XSS Protection ==============
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} javascript\: [NC,OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*iframe.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [NC,OR]
RewriteCond %{QUERY_STRING} (\.\./|\.\.\\) [NC]
RewriteRule ^(.*)$ - [F,L]
# ============== Rate Limiting (Basic) ==============
# Limit request rate to prevent brute force
DOSHashTableSize 3097
DOSPageCount 10
DOSSiteCount 100
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 10
# ============== MIME Type Settings ==============
AddType application/javascript js
AddType application/json json
AddType text/css css
AddType image/svg+xml svg
# ============== Compression ==============
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json
# ============== Cache Control ==============
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/pdf "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
# ============== Prevent Hotlinking ==============
RewriteEngine on
# Uncomment and modify the next line to prevent image hotlinking
# RewriteCond %{HTTP_REFERER} !^$
# RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
# RewriteRule \.(jpg|jpeg|png|gif|svg)$ - [F,NC]
# ================================================================================
# END OF SECURITY CONFIGURATION
# ================================================================================