# Apache Configuration for C++ Learning System

# Enable RewriteEngine
RewriteEngine On

# Force HTTPS (uncomment after SSL is set up)
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# PHP Settings
<IfModule mod_php7.c>
    # Security
    php_flag display_errors Off
    php_flag log_errors On
    php_value error_log error_log.txt

    # Performance
    php_value memory_limit 256M
    php_value max_execution_time 60
    php_value max_input_time 60

    # File Uploads
    php_value upload_max_filesize 10M
    php_value post_max_size 10M

    # Session
    php_value session.gc_maxlifetime 3600
</IfModule>

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

<FilesMatch "^\.gitignore$">
    Order allow,deny
    Deny from all
</FilesMatch>

<FilesMatch "\.(md|log|sql)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Protect config directory
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^config/ - [F,L]
    RewriteRule ^database/ - [F,L]
</IfModule>

# 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

    # 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/x-icon "access plus 1 year"

    # CSS and JavaScript
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"

    # Fonts
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
</IfModule>

# Disable directory browsing
Options -Indexes

# Custom error pages (optional - create these files)
# ErrorDocument 404 /error404.php
# ErrorDocument 403 /error403.php
# ErrorDocument 500 /error500.php

# 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 sniffing
    Header always set X-Content-Type-Options "nosniff"

    # Referrer Policy
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# Protect against DDoS
<IfModule mod_evasive20.c>
    DOSHashTableSize 3097
    DOSPageCount 5
    DOSSiteCount 100
    DOSPageInterval 1
    DOSSiteInterval 1
    DOSBlockingPeriod 10
</IfModule>
