Apache vhost gotchas (icons alias, manifests, SPA fallback, redirects)
apachehttpdvhostpwaspahtaccesscaching
A collection of Apache httpd configuration traps hit while serving PWAs, single-page apps and
PHP APIs on the Alma 9 server (Apache 2.4 on :8008 behind nginx, see
nginx in front of Apache (TLS proxy on :8008)). Each one cost at least one debugging round. Always run
apachectl configtest before systemctl reload httpd.
The global /icons/ alias hijacks every vhost
The stock autoindex.conf contains a server-wide Alias /icons/ /usr/share/httpd/icons/.
Any site that ships its own icons/ directory (Flutter web and most PWAs do) gets 404s for
every icon, on every vhost. Fix by adding a scoped alias inside the vhost:
Alias /icons/ /var/www/html/site/public/icons/
Check this for every new vhost that has an icons/ dir. (tldrnerd fixed 2026-08-11, plumb
2026-10-06.)
Web app manifests served as text/plain
Apache does not know .webmanifest. Add:
AddType application/manifest+json .webmanifest
Single-page apps: refresh 404 on deep routes
A path-routed SPA (Angular, Flutter with path URL strategy) needs every non-file, non-directory
path to return index.html, or reloading /resume 404s:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]
FallbackResource /index.html does the same in one line, but AliasMatch must appear
before FallbackResource or the fallback swallows the aliased paths. Hash-routed apps
(#/route) need none of this.
A real directory inside an SPA's docroot bypasses that fallback (the !-d condition), which
makes it an easy place to bolt on a separate mini-app: hampton.website (Angular) serves an
unrelated checklist page from /broker/. If the SPA deploys with rsync --delete, the extra
directory will be wiped, so check the deploy first.
Base href and subpath deploys
An Angular build with <base href="/"> deployed into /h/ is unloadable: every asset
resolves against the root. Either build with the matching base href or deploy at the root.
(hampton.website had this; /h/* now 301s to the root.)
Header always set overrides PHP
Header always set Cache-Control ... in a vhost replaces whatever the PHP script sent. A blanket
<FilesMatch "\.php$"> Header always set Cache-Control "private, no-store" silently flattened a
script that set its own caching, which caused a 404 to be cached for a day. Exclude such scripts
with a negative lookahead (<FilesMatch "^(?!poster\.php$).*\.php$">) instead of adding a later
<Files> block; <Files>/<FilesMatch> sections apply in config order.
FallbackResource as a router (PHP sites)
On videos.jhampton.io and 3d.jhampton.io a single FallbackResource /series.php (or /index.php) routes pretty URLs like /little-cat-diary to a PHP front controller. It only fires when no real file or directory matches, so /admin/, /assets/..., /sw.js and files added later can never be shadowed by a slug, with no allowlist to maintain. The fallback script is therefore also the site's 404 handler: make it answer real 404s. Any AliasMatch (e.g. /poster/*.jpg) must still come before it.
Section merge order decides which rule wins
Apache merges <Directory> (and .htaccess) first, then <Files>/<FilesMatch>, then <Location>/<LocationMatch>, and <If> sections last. So a <Location> rule for a vendor folder and an <If "%{QUERY_STRING} =~ /^v=/"> rule that marks stamped assets immutable both override a directory-wide default. On justinahampton.com, directories of unbundled ES modules get <Directory ...> Header set Cache-Control "no-cache" so deploys never mix stale modules; see Caching static sites and unbundled ES modules (?v= stamps, no-cache, prefetch).
Making a vhost static-only
To guarantee nothing executes (user-supplied or AI-written content), put <Location "/">
SetHandler none</Location> in the vhost: Location merges after every <Files> block, including
the global php.conf handler, so no file reaches php-fpm. Add AddType text/plain .php .py .sh ...
so scripts display as text, and AllowOverride None. See Hosting one static site per subdomain with a wildcard vhost.
Data directories outside the docroot
Runtime data written by PHP (JSON catalogs, uploads) lives outside the deployed tree (e.g. /var/www/3d-data/), owned by apache, with an httpd_sys_rw_content_t file-context rule (semanage fcontext + restorecon). Rsync deploys then cannot clobber it. See A JSON file as the database (flat-file catalogs).
HTTP to HTTPS redirect that never fires
If an HTTP vhost has a <Directory> with rewrite rules ending in [L], a certbot redirect at
the VirtualHost level never runs. HTTP vhosts should be redirect-only (no DocumentRoot or
Directory). With nginx in front, Apache has no HTTP-redirect vhosts at all.
Cache headers for SPAs
Serve index.html, JS and CSS with no-cache (revalidate) so deploys reach returning visitors;
give only content-hashed or truly static paths a one-year immutable. A file under an immutable
path (a logo in /assets/) can never be changed in place; ship it under a new filename.
CORS for more than one origin (tldr API)
Header set Access-Control-Allow-Origin takes one value, so allowing two sites means echoing the
request's Origin when it matches an allowlist:
SetEnvIf Origin "^https://(tldrnerd\.com|justinahampton\.com)$" CORS_ORIGIN=$0
Header always set Access-Control-Allow-Origin "%{CORS_ORIGIN}e" env=CORS_ORIGIN
- Requests with no
Originheader (curl, server-to-server) get no ACAO header. Smoke tests that check CORS must send-H 'Origin: https://tldrnerd.com'. Header alwaysalso decorates error responses, so a CORS check can pass on a 500.Access-Control-Allow-Methodsis part of the contract: the tldr API allows GET, POST, PUT, DELETE only, so a PATCH route fails preflight in browsers (group rename shipped as PUT for this reason).- A directory served by
Alias(e.g. an old local uploads directory) does not inherit the CORS headers set on the API'spublic/<Directory>; add them there too or images fail in Flutter web. - Every new
api.*vhost must copy the CORS block. On this server CORS lives only in the httpd vhost, never in Slim or nginx. The Forge web app (forge.hampton.website calling api.forge.hampton.website) had none, so the browser blocked every call; the Flutter client saw status 0 and showed "Incorrect username or password" (or a blank home screen with a cached token). Testing through the Android APK missed it because native apps do not enforce CORS. Include the OPTIONS short-circuit (R=204) before the front-controller rewrite, plusVary: Origin. Check: a preflight with an allowedOrigingets 204 with the allow-origin header; a foreignOrigingets none (2026-10-14).
Behind a TLS proxy: canonical names
With nginx terminating TLS, Apache believes requests are plain HTTP, so self-referential redirects
(mod_dir's trailing-slash 301) point at http://. Set ServerName https://example.com and
UseCanonicalName On in the vhost. On tldrnerd.com the http redirect briefly carried a reset token
in cleartext.
Other traps
php_valuein.htaccessis a 500 under php-fpm; see Setting up an Alma Linux 9 LAMP server..htaccessneedsAllowOverride Allon the Directory.- Keep server-only config (
config.php, secrets) outside the DocumentRoot, or verify it 404s after deploy. Put front controllers' DocumentRoot onpublic/. apachectl configtestwarns "DocumentRoot does not exist" when the vhost is installed before the first deploy; harmless.- php-fpm never sees the
Authorizationheader (mod_proxy_fcgi strips it), so bearer-token APIs answer 401 for valid tokens. AddCGIPassAuth Onin the API directory's.htaccess(or the<Directory>block). Hit by the guardian API at tldrcom.com/guardian/api. - Serving an app from a path whose DocumentRoot is a release symlink: put it outside the
symlinked tree and map it with
Alias /guardian /var/www/html/com/guardian/in both the :80 and the-le-ssl.confvhosts, or it disappears when the symlink is flipped for the next release. - Keep downloadable files (APKs) outside another app's docroot if that app deploys with
rsync --delete, and map them in withAlias; jhampton.io serves/keyboard.apkthis way. - Put SQLite databases and uploaded files outside the web tree (e.g.
/var/www/guardian-data/, owned by apache, WAL mode) and serve uploads only through an authenticated script.
See also
- nginx in front of Apache (TLS proxy on :8008)
- SELinux file contexts and booleans on web servers
- Deploy scripts with rsync (patterns and traps)
- Flutter web (renderers, SEO, performance, when not to use it)
Linked from
- Caching static sites and unbundled ES modules (?v= stamps, no-cache, prefetch)
- Deploy scripts with rsync (patterns and traps)
- Flutter web (renderers, SEO, performance, when not to use it)
- Hosting one static site per subdomain with a wildcard vhost
- nginx in front of Apache (TLS proxy on :8008)
- Offline-first browser tools (module workers, precache lists, no build step)
- Open Graph link previews (SPAs, private posts, private-storage media)
- Personal finance dashboards: bank aggregation and subscription detection
- Responding to a critical PHP security release (and what to do with an unpatchable server)
- SELinux file contexts and booleans on web servers
- Server-minted claim codes for in-game rewards (never ship the secret)
- Setting up an Alma Linux 9 LAMP server
- Slim 4 PHP APIs (and migrating from Limonade)
- Taking .php out of page addresses without breaking old links
- videos.jhampton.io (private video site architecture)