📚 Encyclopedia

Servers & Hosting · updated 2026-10-26

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

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

See also

Linked from

Sources: tldr memory (feature-gold-coins, server-web-performance, completed-work), global CLAUDE.md server notes, phone memory (guardian-remote-management), keyboard memory (history), plumb memory (gotchas, deployment), hampton-website memory (deploy), infra memory (server-setup-gotchas), justinahampton memory (videos-urls-and-previews, 3d-subdomain, joyride-architecture), tldr post #1555, dad memory (broker-checklist-site), tldr memory (feature-ai-guest-mode)