# Bundled web-root protection (Apache 2.4; the vhost example enables it with "AllowOverride All").
# nginx users: deploy/nginx.conf.example contains the equivalent rules.
# VERIFY after every deploy:   curl -sI https://YOUR-HOST/.env     -> must be 403 or 404

Options -Indexes -MultiViews

# Never serve dotfiles (.env, .git, .gitignore ...) or secrets/config/source-ish file types
<FilesMatch "^\.">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>
<FilesMatch "\.(env|sql|md|log|lock|json|sh|ini|example|bak|orig|dist|yml|yaml|zip)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

# Application internals: never reachable over HTTP (also enforced by a .htaccess inside most of these folders;
# this rule additionally covers vendor/, which Composer recreates on every install).
<IfModule mod_alias.c>
    RedirectMatch 403 "^/(vendor|includes|config|migrations|bin|tests|deploy|updates|storage)(/|$)"
</IfModule>

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

# The static login page: never frameable (clickjacking), no sniffing, no third-party resources.
<IfModule mod_headers.c>
    <FilesMatch "^(login|portal)\.html$">
        Header always set X-Frame-Options "DENY"
        Header always set Referrer-Policy "no-referrer"
        Header always set Cache-Control "no-store"
        Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; font-src 'self'; img-src 'self' data:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; object-src 'none'"
    </FilesMatch>
</IfModule>

# Apps hosted from the apps/ folder (https://domain/<Name>/): anything that is not a real file or folder goes to the login gate,
# which only answers for folders in apps/ (everything else: 404). apps/ itself is closed by apps/.htaccess.
<IfModule mod_rewrite.c>
    RewriteEngine On
    # cPanel / PHP-FPM / CGI hosts drop the Authorization header unless it is passed on explicitly
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteRule ^apps/ gate.php [L,QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ gate.php [L,QSA]
</IfModule>
