# ==================================================================================================
# Candivo - Apache configuration
# --------------------------------------------------------------------------------------------------
# Routes every request that is not a real file to index.php and keeps the application's internal
# folders private. The application sets its own security headers from PHP, so it stays protected on
# servers that ignore this file (nginx: see docs/nginx.conf.sample; IIS: web.config) - the installer
# and the Security Center actively probe that the private folders are really unreachable.
# ==================================================================================================

Options -Indexes -MultiViews
DirectoryIndex index.php

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Internal folders and dot-files are never served.
    RewriteRule ^(app|config|database|lang|routes|storage|templates|tools|vendor|docs)(/|$) - [F,L]
    RewriteRule (^|/)\.(?!well-known/) - [F,L]
    RewriteRule \.(sql|md|log|bak|old|sh|ini|dist|lock)$ - [F,L]
    # the signed list of the release says which version is installed and what every file is: not for visitors
    RewriteRule ^RELEASE\.(json|sig)$ - [F,L]

    # Real files and folders (assets/, install/) are served as they are; everything else is the app.
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^ index.php [L,QSA]
</IfModule>

# Even if mod_rewrite is unavailable, these files must not be readable.
<FilesMatch "^(config\.php|config\.sample\.php|composer\.(json|lock)|\.env.*|RELEASE\.(json|sig)|.*\.(sql|md|log|bak|old|sh|ini|dist))$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always unset X-Powered-By
</IfModule>

# Versioned assets (?v=mtime) can be cached for a year; pages never.
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css               "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType text/javascript        "access plus 1 year"
    ExpiresByType font/woff2             "access plus 1 year"
    ExpiresByType image/svg+xml          "access plus 1 month"
    ExpiresByType image/png              "access plus 1 month"
    ExpiresByType image/webp             "access plus 1 month"
    ExpiresByType text/html              "access plus 0 seconds"
</IfModule>

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

# PHP hardening, where the host allows per-directory overrides (wrapped: a bare php_flag is a 500 on PHP-FPM).
<IfModule mod_php.c>
    php_flag display_errors off
    php_flag log_errors on
    php_flag expose_php off
    php_value upload_max_filesize 12M
    php_value post_max_size 16M
</IfModule>
