# Apache TLS forwarding for Next.js

On 25 November 2026 the live HTTPS homepage and resource pages returned HTTP
308 with a Location identical to the requested URL. Adding
`X-Forwarded-Proto: https` to a diagnostic request returned HTTP 200. Apache
terminated TLS but did not supply the original scheme to the Next.js listener.
Next.js therefore inferred HTTP from its internal connection.

The application proxy now upgrades only a known public HTTP connection on port
80. An internal HTTP listener on port 3000 or another nonstandard port does not
establish that the visitor used HTTP. The www hostname still redirects to the
canonical HTTPS hostname, preserving the path and query.

## Immediate server correction

In the **existing HTTPS VirtualHost for cambridgeveritas.com**, before its
ProxyPass directives, set these headers. Use `set`, so client-supplied values
are replaced:

```apache
# Inside the existing <VirtualHost *:443> only:
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"
```

Do not set these HTTPS values globally or in the HTTP VirtualHost. If the
`headers` module is not enabled, enable it with `sudo a2enmod headers`.
Keep the existing certificate, proxy destinations and authentication settings.

The port-80 VirtualHost should redirect public HTTP traffic to HTTPS at Apache.
For a VirtualHost dedicated to the main site and its www alias, the existing
HTTP-to-HTTPS redirect can use:

```apache
# Inside the main site's existing <VirtualHost *:80> only:
Redirect permanent / https://cambridgeveritas.com/
```

Preserve any certificate-renewal challenge exceptions and existing equivalent
redirect. Do not replace a shared VirtualHost or create a duplicate one.
If HTTP requests intentionally reach Next.js instead, overwrite the forwarding
headers there with `X-Forwarded-Proto: http` and `X-Forwarded-Port: 80`.

Validate the configuration and reload gracefully:

```sh
sudo apachectl configtest
# Run only after the configuration test reports Syntax OK:
sudo systemctl reload apache2
```

The HTTPS forwarding correction works with the currently deployed app and does
not require a Next.js rebuild. Deploy the updated proxy.ts through the normal
build/restart workflow as a regression safeguard.

## Verification

Without sending any custom forwarding headers, verify that:

- `https://cambridgeveritas.com/` returns 200.
- `https://cambridgeveritas.com/cvcac/resources` returns 200.
- HTTP and www requests redirect to the canonical HTTPS URL and then terminate.
- Existing lesson paths and query strings are preserved.

Application regression check: `node scripts/test-public-redirects.mjs`.
With a rebuilt local server, add `--base=http://127.0.0.1:3100`.

References:

- [Apache RequestHeader directive](https://httpd.apache.org/docs/2.4/mod/mod_headers.html#requestheader)
- [Next.js self-hosting and reverse proxies](https://nextjs.org/docs/app/guides/self-hosting#reverse-proxy)
