From 66619d78b18309055b0ab8dac7a0a0784553f914 Mon Sep 17 00:00:00 2001 From: Michael Aguiar Date: Tue, 29 Sep 2026 07:57:31 -0700 Subject: [PATCH] Serve WordPress from the alios share host in wordpress-dev images WordPress stores its URL in the database, so a site reached through an `alios share` tunnel redirects visitors back to https://.test. The wordpress-dev variant now ships an auto_prepend_file that, only for requests the alios share vhost marks with `X-Alios-Share: 1`, filters home/siteurl to the forwarded public host (via pre-initialized hooks, so it also wins over WP_HOME/WP_SITEURL) and rewrites the stored host in text responses and Location headers. Normal .test requests and WP-CLI are untouched, nothing is written to the project, and the production wordpress variant is unchanged. The PHP is inlined as a Dockerfile heredoc so editing it triggers this variant's CI rebuild (change detection only watches the Dockerfiles). Co-Authored-By: Claude Opus 5.5 --- README.md | 11 +++ php/8.2/frankenphp/Dockerfile.wordpress-dev | 100 ++++++++++++++++++++ php/8.3/frankenphp/Dockerfile.wordpress-dev | 100 ++++++++++++++++++++ php/8.4/frankenphp/Dockerfile.wordpress-dev | 100 ++++++++++++++++++++ php/8.5/frankenphp/Dockerfile.wordpress-dev | 100 ++++++++++++++++++++ 5 files changed, 411 insertions(+) diff --git a/README.md b/README.md index 01b1a28..9971412 100644 --- a/README.md +++ b/README.md @@ -62,3 +62,14 @@ The `frankenphp:wordpress` variant renders a Caddy hardening snippet at containe All of the above are overridable via environment variables if a site needs different values. The `wordpress` variant also ships defaults for the [static asset caching](#static-asset-caching) snippet covering both the Bedrock (`/app/...`, `/wp/...`) and stock (`/wp-content/...`, `/wp-includes/...`) layouts, including cache-plugin output and Avada's compiled `fusion-styles` / `fusion-scripts` directories. + +## Sharing a local WordPress site (`wordpress-dev` only) + +WordPress stores its URL in the database, so a site reached through an [`alios share`](https://github.com/aliasproject/alios-cli) tunnel would redirect every visitor back to `https://.test`. The `wordpress-dev` variant ships a PHP `auto_prepend_file` (`/usr/local/lib/php/alios-share.php`) that fixes this without touching the project. + +It only activates when the request carries `X-Alios-Share: 1`. The alios share vhost sets that header itself, so visitors can't forge or strip it. For those requests it: + +- filters `home` / `siteurl` to the forwarded public host, keeping any path (e.g. Bedrock's `/wp`). This wins over `WP_HOME` / `WP_SITEURL` defines. +- rewrites the site's stored host to the public one in HTML, JSON, JS, CSS and XML responses and in `Location` headers. That covers post content, `srcset`s and REST output, and upgrades `http://` links to `https://`. + +Requests without the header, including every normal `.test` request and WP-CLI, are untouched. The production `wordpress` variant doesn't include it. diff --git a/php/8.2/frankenphp/Dockerfile.wordpress-dev b/php/8.2/frankenphp/Dockerfile.wordpress-dev index 6ef3eca..df792ad 100644 --- a/php/8.2/frankenphp/Dockerfile.wordpress-dev +++ b/php/8.2/frankenphp/Dockerfile.wordpress-dev @@ -18,4 +18,104 @@ ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS=1 \ # locally. Composer itself is already present via the base image. ENV PHP_DISABLE_FUNCTIONS= +# `alios share` support. WordPress stores its URL in the database, so a site +# reached through a share tunnel redirects every visitor back to +# https://.test. The alios share vhost marks tunnelled requests with +# `X-Alios-Share: 1`. For those requests only, this prepend file serves +# WordPress from the forwarded public host by filtering home/siteurl. It also +# rewrites the stored host in the response body (post content, srcsets, JSON) +# and in Location headers. Requests without the header, including every normal +# .test request and WP-CLI, are untouched. Nothing is written to the project, +# and it only exists in this dev image, so it can never reach production. +# Inline (not a COPY'd file) so editing it triggers this image's CI rebuild. +COPY --chmod=644 <<'PHP' /usr/local/lib/php/alios-share.php +.test/wp). + $swap = function ($value) use ($shared, $host, &$originals) { + if (!is_string($value) || $value === '') { + return $value; + } + $parts = parse_url($value); + if (empty($parts['host'])) { + return $value; + } + $original = $parts['host'] . (isset($parts['port']) ? ':' . $parts['port'] : ''); + if (strtolower($original) !== $host) { + $originals[$original] = true; + } + return $shared . ($parts['path'] ?? ''); + }; + + // Registered before WordPress loads, via its pre-initialized hooks + // support. The late priority runs after core's _config_wp_home / + // _config_wp_siteurl, so this also wins over WP_HOME / WP_SITEURL defines. + foreach (['option_home', 'option_siteurl'] as $hook) { + $GLOBALS['wp_filter'][$hook][9999][] = ['function' => $swap, 'accepted_args' => 1]; + } + + // Point absolute (and JSON-escaped) links at the shared host, upgrading + // http:// since the tunnel is https-only and content saved before the + // site had SSL would otherwise be blocked as mixed content. + $rewrite = function ($text) use ($host, &$originals) { + $from = []; + $to = []; + foreach (array_keys($originals) as $original) { + foreach (['http', 'https'] as $scheme) { + $from[] = $scheme . '://' . $original; + $to[] = 'https://' . $host; + $from[] = $scheme . ':\/\/' . $original; + $to[] = 'https:\/\/' . $host; + } + $from[] = '//' . $original; + $to[] = '//' . $host; + } + return str_ireplace($from, $to, $text); + }; + + ob_start(function ($buffer) use ($rewrite, &$originals) { + if (!$originals || $buffer === '') { + return $buffer; + } + foreach (headers_list() as $header) { + if (stripos($header, 'content-type:') === 0 + && !preg_match('#(text/|json|javascript|xml)#i', $header)) { + return $buffer; + } + } + return $rewrite($buffer); + }); + + header_register_callback(function () use ($rewrite) { + foreach (headers_list() as $header) { + if (stripos($header, 'location:') === 0) { + header('Location: ' . $rewrite(trim(substr($header, 9)))); + } + } + // The body may have changed length after rewriting. + header_remove('Content-Length'); + }); +})(); +PHP + +RUN echo "auto_prepend_file = /usr/local/lib/php/alios-share.php" > "$PHP_INI_DIR/conf.d/zz-alios-share.ini" + USER appuser diff --git a/php/8.3/frankenphp/Dockerfile.wordpress-dev b/php/8.3/frankenphp/Dockerfile.wordpress-dev index dcdc054..5378933 100644 --- a/php/8.3/frankenphp/Dockerfile.wordpress-dev +++ b/php/8.3/frankenphp/Dockerfile.wordpress-dev @@ -18,4 +18,104 @@ ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS=1 \ # locally. Composer itself is already present via the base image. ENV PHP_DISABLE_FUNCTIONS= +# `alios share` support. WordPress stores its URL in the database, so a site +# reached through a share tunnel redirects every visitor back to +# https://.test. The alios share vhost marks tunnelled requests with +# `X-Alios-Share: 1`. For those requests only, this prepend file serves +# WordPress from the forwarded public host by filtering home/siteurl. It also +# rewrites the stored host in the response body (post content, srcsets, JSON) +# and in Location headers. Requests without the header, including every normal +# .test request and WP-CLI, are untouched. Nothing is written to the project, +# and it only exists in this dev image, so it can never reach production. +# Inline (not a COPY'd file) so editing it triggers this image's CI rebuild. +COPY --chmod=644 <<'PHP' /usr/local/lib/php/alios-share.php +.test/wp). + $swap = function ($value) use ($shared, $host, &$originals) { + if (!is_string($value) || $value === '') { + return $value; + } + $parts = parse_url($value); + if (empty($parts['host'])) { + return $value; + } + $original = $parts['host'] . (isset($parts['port']) ? ':' . $parts['port'] : ''); + if (strtolower($original) !== $host) { + $originals[$original] = true; + } + return $shared . ($parts['path'] ?? ''); + }; + + // Registered before WordPress loads, via its pre-initialized hooks + // support. The late priority runs after core's _config_wp_home / + // _config_wp_siteurl, so this also wins over WP_HOME / WP_SITEURL defines. + foreach (['option_home', 'option_siteurl'] as $hook) { + $GLOBALS['wp_filter'][$hook][9999][] = ['function' => $swap, 'accepted_args' => 1]; + } + + // Point absolute (and JSON-escaped) links at the shared host, upgrading + // http:// since the tunnel is https-only and content saved before the + // site had SSL would otherwise be blocked as mixed content. + $rewrite = function ($text) use ($host, &$originals) { + $from = []; + $to = []; + foreach (array_keys($originals) as $original) { + foreach (['http', 'https'] as $scheme) { + $from[] = $scheme . '://' . $original; + $to[] = 'https://' . $host; + $from[] = $scheme . ':\/\/' . $original; + $to[] = 'https:\/\/' . $host; + } + $from[] = '//' . $original; + $to[] = '//' . $host; + } + return str_ireplace($from, $to, $text); + }; + + ob_start(function ($buffer) use ($rewrite, &$originals) { + if (!$originals || $buffer === '') { + return $buffer; + } + foreach (headers_list() as $header) { + if (stripos($header, 'content-type:') === 0 + && !preg_match('#(text/|json|javascript|xml)#i', $header)) { + return $buffer; + } + } + return $rewrite($buffer); + }); + + header_register_callback(function () use ($rewrite) { + foreach (headers_list() as $header) { + if (stripos($header, 'location:') === 0) { + header('Location: ' . $rewrite(trim(substr($header, 9)))); + } + } + // The body may have changed length after rewriting. + header_remove('Content-Length'); + }); +})(); +PHP + +RUN echo "auto_prepend_file = /usr/local/lib/php/alios-share.php" > "$PHP_INI_DIR/conf.d/zz-alios-share.ini" + USER appuser diff --git a/php/8.4/frankenphp/Dockerfile.wordpress-dev b/php/8.4/frankenphp/Dockerfile.wordpress-dev index d2e8a3c..0d3de10 100644 --- a/php/8.4/frankenphp/Dockerfile.wordpress-dev +++ b/php/8.4/frankenphp/Dockerfile.wordpress-dev @@ -18,4 +18,104 @@ ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS=1 \ # locally. Composer itself is already present via the base image. ENV PHP_DISABLE_FUNCTIONS= +# `alios share` support. WordPress stores its URL in the database, so a site +# reached through a share tunnel redirects every visitor back to +# https://.test. The alios share vhost marks tunnelled requests with +# `X-Alios-Share: 1`. For those requests only, this prepend file serves +# WordPress from the forwarded public host by filtering home/siteurl. It also +# rewrites the stored host in the response body (post content, srcsets, JSON) +# and in Location headers. Requests without the header, including every normal +# .test request and WP-CLI, are untouched. Nothing is written to the project, +# and it only exists in this dev image, so it can never reach production. +# Inline (not a COPY'd file) so editing it triggers this image's CI rebuild. +COPY --chmod=644 <<'PHP' /usr/local/lib/php/alios-share.php +.test/wp). + $swap = function ($value) use ($shared, $host, &$originals) { + if (!is_string($value) || $value === '') { + return $value; + } + $parts = parse_url($value); + if (empty($parts['host'])) { + return $value; + } + $original = $parts['host'] . (isset($parts['port']) ? ':' . $parts['port'] : ''); + if (strtolower($original) !== $host) { + $originals[$original] = true; + } + return $shared . ($parts['path'] ?? ''); + }; + + // Registered before WordPress loads, via its pre-initialized hooks + // support. The late priority runs after core's _config_wp_home / + // _config_wp_siteurl, so this also wins over WP_HOME / WP_SITEURL defines. + foreach (['option_home', 'option_siteurl'] as $hook) { + $GLOBALS['wp_filter'][$hook][9999][] = ['function' => $swap, 'accepted_args' => 1]; + } + + // Point absolute (and JSON-escaped) links at the shared host, upgrading + // http:// since the tunnel is https-only and content saved before the + // site had SSL would otherwise be blocked as mixed content. + $rewrite = function ($text) use ($host, &$originals) { + $from = []; + $to = []; + foreach (array_keys($originals) as $original) { + foreach (['http', 'https'] as $scheme) { + $from[] = $scheme . '://' . $original; + $to[] = 'https://' . $host; + $from[] = $scheme . ':\/\/' . $original; + $to[] = 'https:\/\/' . $host; + } + $from[] = '//' . $original; + $to[] = '//' . $host; + } + return str_ireplace($from, $to, $text); + }; + + ob_start(function ($buffer) use ($rewrite, &$originals) { + if (!$originals || $buffer === '') { + return $buffer; + } + foreach (headers_list() as $header) { + if (stripos($header, 'content-type:') === 0 + && !preg_match('#(text/|json|javascript|xml)#i', $header)) { + return $buffer; + } + } + return $rewrite($buffer); + }); + + header_register_callback(function () use ($rewrite) { + foreach (headers_list() as $header) { + if (stripos($header, 'location:') === 0) { + header('Location: ' . $rewrite(trim(substr($header, 9)))); + } + } + // The body may have changed length after rewriting. + header_remove('Content-Length'); + }); +})(); +PHP + +RUN echo "auto_prepend_file = /usr/local/lib/php/alios-share.php" > "$PHP_INI_DIR/conf.d/zz-alios-share.ini" + USER appuser diff --git a/php/8.5/frankenphp/Dockerfile.wordpress-dev b/php/8.5/frankenphp/Dockerfile.wordpress-dev index 3e866eb..af2cae8 100644 --- a/php/8.5/frankenphp/Dockerfile.wordpress-dev +++ b/php/8.5/frankenphp/Dockerfile.wordpress-dev @@ -18,4 +18,104 @@ ENV PHP_OPCACHE_VALIDATE_TIMESTAMPS=1 \ # locally. Composer itself is already present via the base image. ENV PHP_DISABLE_FUNCTIONS= +# `alios share` support. WordPress stores its URL in the database, so a site +# reached through a share tunnel redirects every visitor back to +# https://.test. The alios share vhost marks tunnelled requests with +# `X-Alios-Share: 1`. For those requests only, this prepend file serves +# WordPress from the forwarded public host by filtering home/siteurl. It also +# rewrites the stored host in the response body (post content, srcsets, JSON) +# and in Location headers. Requests without the header, including every normal +# .test request and WP-CLI, are untouched. Nothing is written to the project, +# and it only exists in this dev image, so it can never reach production. +# Inline (not a COPY'd file) so editing it triggers this image's CI rebuild. +COPY --chmod=644 <<'PHP' /usr/local/lib/php/alios-share.php +.test/wp). + $swap = function ($value) use ($shared, $host, &$originals) { + if (!is_string($value) || $value === '') { + return $value; + } + $parts = parse_url($value); + if (empty($parts['host'])) { + return $value; + } + $original = $parts['host'] . (isset($parts['port']) ? ':' . $parts['port'] : ''); + if (strtolower($original) !== $host) { + $originals[$original] = true; + } + return $shared . ($parts['path'] ?? ''); + }; + + // Registered before WordPress loads, via its pre-initialized hooks + // support. The late priority runs after core's _config_wp_home / + // _config_wp_siteurl, so this also wins over WP_HOME / WP_SITEURL defines. + foreach (['option_home', 'option_siteurl'] as $hook) { + $GLOBALS['wp_filter'][$hook][9999][] = ['function' => $swap, 'accepted_args' => 1]; + } + + // Point absolute (and JSON-escaped) links at the shared host, upgrading + // http:// since the tunnel is https-only and content saved before the + // site had SSL would otherwise be blocked as mixed content. + $rewrite = function ($text) use ($host, &$originals) { + $from = []; + $to = []; + foreach (array_keys($originals) as $original) { + foreach (['http', 'https'] as $scheme) { + $from[] = $scheme . '://' . $original; + $to[] = 'https://' . $host; + $from[] = $scheme . ':\/\/' . $original; + $to[] = 'https:\/\/' . $host; + } + $from[] = '//' . $original; + $to[] = '//' . $host; + } + return str_ireplace($from, $to, $text); + }; + + ob_start(function ($buffer) use ($rewrite, &$originals) { + if (!$originals || $buffer === '') { + return $buffer; + } + foreach (headers_list() as $header) { + if (stripos($header, 'content-type:') === 0 + && !preg_match('#(text/|json|javascript|xml)#i', $header)) { + return $buffer; + } + } + return $rewrite($buffer); + }); + + header_register_callback(function () use ($rewrite) { + foreach (headers_list() as $header) { + if (stripos($header, 'location:') === 0) { + header('Location: ' . $rewrite(trim(substr($header, 9)))); + } + } + // The body may have changed length after rewriting. + header_remove('Content-Length'); + }); +})(); +PHP + +RUN echo "auto_prepend_file = /usr/local/lib/php/alios-share.php" > "$PHP_INI_DIR/conf.d/zz-alios-share.ini" + USER appuser