# File: .htaccess, the web server rules of a site root.
# Purpose: sends every URL that is not a real file to index.php, answers the core 404 page for the 5 private paths core,
# config, assets/plugins, assets/themes and assets/storage and every assets/uploads/private-* folder, and carries the 5 security headers.
# Defines: nothing; Apache directives
# Doc: docs/notes/updates.plexcms.com/packages/plexcms-core/.htaccess.md

Options -Indexes
DirectoryIndex index.php

# A refused or missing path reaches index.php with the path the visitor asked for, and the core answers its 404 page
# with status 404: the RewriteRule refusals below send 404, and a Require refusal of this folder or a subfolder sends 403.
ErrorDocument 403 /index.php
ErrorDocument 404 /index.php

<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
    Header set Permissions-Policy "geolocation=(), microphone=(), camera=(), interest-cohort=()"
    # The header is sent over HTTPS only: a copy whose certificate is not in place yet stays reachable.
    <If "%{HTTPS} == 'on'">
        Header always set Strict-Transport-Security "max-age=15552000"
    </If>
</IfModule>

# Private file types. The folder rules below deny every private PHP file by path;
# the config folder rule covers config/plex-config.php.
<FilesMatch "\.(sql|md|log|ini|bak|yml|yaml|lock|sh|key|env)$">
    Require all denied
</FilesMatch>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # The ErrorDocument pass of a refused or missing path reaches index.php with status 403 or 404; no rule below
    # redirects it, and the core answers its 404 page for the path the visitor asked for.
    RewriteCond %{ENV:REDIRECT_STATUS} ^40[34]$
    RewriteRule ^index\.php$ - [L]

    # The certificate authority folder passes first; every other dot path answers 404,
    # .htaccess and the plugin dot folders of assets/plugins among them.
    RewriteRule ^\.well-known/ - [L]
    RewriteRule ^\. - [R=404,L]
    RewriteRule /\. - [R=404,L]

    # Bearer tokens of the api area reach PHP through this variable.
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # The 5 private paths, every file in them. Their static files leave through the
    # core route /plexcms/assets/<package>/<path>.
    RewriteRule ^(core|config|assets/plugins|assets/themes|assets/storage)(/|$) - [R=404,L]

    # index.php is the only valid direct target among the root files; a path after the name, PATH_INFO, is refused too.
    RewriteRule ^(plexcms-load|manifest)\.php(/|$) - [R=404,L]

    # Uploaded files are served by the web server, and none of them runs as a script: a PHP, CGI, Perl, Python
    # or server-side include extension anywhere in a name is refused, x.php.jpg included, in any letter case.
    RewriteRule ^assets/uploads/.*\.(php|phtml|phar|php[0-9]|pht|phps|shtml|cgi|pl|py)(\.|/|$) - [NC,R=404,L]

    # A folder named private-<plugin>-<purpose> holds staff-only files, and its plugin serves them through
    # a route with a capability check.
    RewriteRule ^assets/uploads/private-[^/]+/ - [R=404,L]

    # Force HTTPS. The proxy header is read too: a copy behind a load balancer
    # cannot redirect to itself forever, and a local copy still runs.
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP:X-Forwarded-Proto} !=https
    RewriteCond %{HTTP_HOST} !^localhost [NC]
    RewriteCond %{HTTP_HOST} !^127\. [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Every URL that is not a real file reaches index.php.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 30 days"
    ExpiresByType application/javascript "access plus 30 days"
    ExpiresByType font/woff2 "access plus 30 days"
    ExpiresByType image/png "access plus 30 days"
    ExpiresByType image/jpeg "access plus 30 days"
    ExpiresByType image/webp "access plus 30 days"
    ExpiresByType image/svg+xml "access plus 30 days"
</IfModule>

# No error text reaches a visitor where mod_php is the handler.
<IfModule mod_php.c>
    php_flag display_errors Off
</IfModule>
