Skip to content

Add --adapt-scheme flag to rewrite HTTPS URLs to HTTP in server responses#94

Merged
swissspidy merged 3 commits intomainfrom
copilot/fix-http-to-https-links
Mar 15, 2026
Merged

Add --adapt-scheme flag to rewrite HTTPS URLs to HTTP in server responses#94
swissspidy merged 3 commits intomainfrom
copilot/fix-http-to-https-links

Conversation

Copy link
Contributor

Copilot AI commented Mar 15, 2026

When a site is configured with HTTPS URLs in the database, wp server (which runs on plain HTTP) leaves HTTPS URLs in responses that bypass the existing option_home/option_siteurl filters — e.g. attachment URLs, site icon URLs, and hardcoded post content.

Changes

  • src/Server_Command.php — Adds [--adapt-scheme] flag. When set, calls putenv('WPCLI_SERVER_ADAPT_SCHEME=1') before proc_open() so the child PHP process inherits it.

  • router.php — When WPCLI_SERVER_ADAPT_SCHEME is set, registers an ob_start() callback that replaces https://<original-host>http://<server-host> across the full response. Uses $GLOBALS['wpcli_server_original_url'] (already populated by the option_home filter) to identify the original host. No-ops gracefully when WordPress hasn't loaded (e.g. static PHP files).

  • features/server.feature — New scenario: configures a WP install with HTTPS siteurl/home, injects a hardcoded HTTPS URL via an mu-plugin wp_head filter (bypassing all existing option filters), and asserts the response contains only http:// URLs.

Usage

# Site was configured with https://example.com; serve locally over HTTP with adapted links
wp server --adapt-scheme

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • example.com
    • Triggering command: /usr/bin/php /usr/bin/php /home/REDACTED/work/server-command/server-command/vendor/wp-cli/wp-cli/bin/../php/boot-fs.php core install --url=REDACTED --title=WP CLI Site --admin_user=admin --admin_email=admin@example.com --admin_password=password1 --skip-email (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Server on http:// returns https:// links</issue_title>
<issue_description>If you have site on https and run php server on http the function get_site_icon_url return a link with https.

I think the problem here https://github.com/WordPress/WordPress/blob/master/wp-includes/general-template.php#L810, because WordPress should use here get_site_url() instead get_option( 'site_icon' ).

Maybe we can add filter on get_site_icon_url to fix it problem or we should fix it in WordPress?</issue_description>

<agent_instructions>Add an adapt-scheme flag</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@swissspidy Worth noting that `get_option( 'site_icon' )` just holds the ID of the site icon, not its URL. The actual URL is generated by `wp_get_attachment_image_url()` and then `wp_get_attachment_url()`, which uses the image URL stored in the attachment metadata in the database.

So the right filter in that case would probably be wp_get_attachment_url.</comment_new>
<comment_new>@schlessera
I'm not sure this can be easily solved. You still have all the links that are stored throughout post content fields that point to https://.

Maybe a better approach would be to add a flag --adapt-scheme that filters the actual output to replace all https:// links pointing to the local server with http:// links?</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…ponses

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix server on http:// returning https:// links Add --adapt-scheme flag to rewrite HTTPS URLs to HTTP in server responses Mar 15, 2026
Copilot AI requested a review from swissspidy March 15, 2026 07:49
@codecov
Copy link

codecov bot commented Mar 15, 2026

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Server_Command.php 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy marked this pull request as ready for review March 15, 2026 08:32
@swissspidy swissspidy requested a review from a team as a code owner March 15, 2026 08:32
Copilot AI review requested due to automatic review settings March 15, 2026 08:32
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an --adapt-scheme flag to wp server to rewrite HTTPS links back to HTTP in served responses, addressing cases where HTTPS URLs come from sources that bypass existing home/siteurl option filters (e.g., attachment metadata, hardcoded content).

Changes:

  • Add --adapt-scheme flag to wp server and pass enablement to the child PHP process via WPCLI_SERVER_ADAPT_SCHEME.
  • Add an output-buffer callback in router.php that rewrites https://<original-host> to http://<current-host> across the response body.
  • Add a Behat scenario asserting HTTPS URLs are rewritten to HTTP when the flag is enabled.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/Server_Command.php Introduces --adapt-scheme and sets an env var before starting the PHP built-in server process.
router.php Adds response-body rewriting via ob_start() when WPCLI_SERVER_ADAPT_SCHEME is present.
features/server.feature Adds an integration scenario verifying rewritten URLs in output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +126 to +130
return str_replace(
'https://' . $original_host,
'http://' . $_SERVER['HTTP_HOST'],
$buffer
);
Comment on lines +125 to +129
$original_host = _get_full_host( $GLOBALS['wpcli_server_original_url'] );
return str_replace(
'https://' . $original_host,
'http://' . $_SERVER['HTTP_HOST'],
$buffer
*
* @param array<string> $args Positional arguments passed through to the PHP binary.
* @param array{host: string, port: string, docroot?: string, config?: string} $assoc_args Associative arguments passed to the command.
* @param array{host: string, port: string, docroot?: string, config?: string, 'adapt-scheme'?: bool} $assoc_args Associative arguments passed to the command.
@swissspidy swissspidy merged commit e81dc13 into main Mar 15, 2026
64 checks passed
@swissspidy swissspidy deleted the copilot/fix-http-to-https-links branch March 15, 2026 08:37
@swissspidy swissspidy added this to the 2.0.17 milestone Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:server Related to 'server' command scope:distribution Related to distribution state:unconfirmed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server on http:// returns https:// links

3 participants