Options -Indexes
Options -MultiViews

# ===== SECURITY HEADERS =====
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set Permissions-Policy "geolocation=(), microphone=(), camera=()"
    Header unset Server
    Header always set Cache-Control "no-cache, no-store, must-revalidate" "expr=%{REQUEST_URI} =~ m#/admin#"
</IfModule>

# ===== PHP =====
<IfModule mod_php8.c>
    php_value upload_max_filesize 15M
    php_value post_max_size 15M
    php_value memory_limit 128M
    php_value max_execution_time 60
    php_flag display_errors off
</IfModule>

# ===== REWRITE ENGINE =====
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Block access to sensitive files
    RewriteRule ^application/ - [F,L]
    RewriteRule ^storage/uploads/cvs/ - [F,L]
    # Block direct PHP file access (except index.php which Apache handles internally)
    RewriteRule ^(?!index\.php$).*\.php$ - [F,L]

    # Allow direct file/directory access
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Route everything through index.php
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

# ===== BLOCK SENSITIVE FILES =====
<FilesMatch "\.(env|log|sql|bak|config|ini|sh|htpasswd)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# ===== CACHE CONTROL =====
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png  "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/gif  "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 application/pdf "access plus 1 week"
    ExpiresByType image/x-icon "access plus 1 year"
</IfModule>

# ===== GZIP COMPRESSION =====
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css application/javascript text/javascript application/json text/xml
</IfModule>

# ===== PROTECT UPLOADS (no PHP execution) =====
<Directory "storage/uploads">
    <FilesMatch "\.php$">
        Order allow,deny
        Deny from all
    </FilesMatch>
</Directory>

# ===== CUSTOM ERROR PAGES =====
ErrorDocument 404 /index.php?url=404
ErrorDocument 403 /index.php?url=403
