Replace manual URL construction from $_SERVER['HTTP_HOST'] with network_home_url().#10939
Replace manual URL construction from $_SERVER['HTTP_HOST'] with network_home_url().#10939ootwch wants to merge 6 commits intoWordPress:6.9from
$_SERVER['HTTP_HOST'] with network_home_url().#10939Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @=, @ootwch. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
0ee2fcd to
8908e8b
Compare
|
I don't believe this will work as intended. For example, the If the network doesn't exist at the root, then this would result in duplicated path segment. |
Replace manual URL construction from $_SERVER['HTTP_HOST'] with network_home_url() across 11 core files. This fixes WordPress behind reverse proxies that rewrite paths (e.g. branch-based staging). Also upgrades wp_redirect() to wp_safe_redirect() in auth_redirect() and removes a dead $scheme variable in class-wp-recovery-mode.php. Trac ticket: https://core.trac.wordpress.org/ticket/53998
8908e8b to
847045c
Compare
Understood, I think. In my setup (CI dev environment, not production) that does not seem to happen because the reverse proxy corrects this. Cursor recommends a dedicated utility function to fix this; I can adapt the CR and verify this in my own setup, but this is then more than than the "just fix a couple of lines" change I had originally intended.. and my tests will not be encompassing enough. Still, I think the original issue still exists (and has existed for a long time); would be nice to get some improvements for this into core, perhaps. |
|
What about the |
**Built by cursorAI - I can (and will) run my test suite later, but I do not have the knowledge to identify side effects. ** Tested only for the case where the install is behind a reverse proxy. Introduces wp_get_current_request_url() in link-template.php, a helper that builds the current request URL using the DB-configured host from home_url() instead of raw $_SERVER['HTTP_HOST']. The helper detects whether REQUEST_URI already includes the home path (standard setup — just swaps scheme+host) or is missing it (reverse proxy stripped a prefix — prepends via home_url()). All call sites that previously concatenated HTTP_HOST + REQUEST_URI now use this helper. SSL-forcing redirects (wp-login.php, pluggable.php) explicitly wrap with set_url_scheme(..., 'https') to preserve the upstream guarantee of HTTPS enforcement. See https://core.trac.wordpress.org/ticket/53998
get_self_link() in feed.php was a feed-specific predecessor of the same pattern centralised in wp_get_current_request_url(). It already avoided $_SERVER['HTTP_HOST'] by taking the host from home_url(), but it still concatenated the raw $_SERVER['REQUEST_URI'] directly — so behind a path-stripping reverse proxy the branch/subdirectory prefix was lost. Replace the manual host-extraction + concatenation with a single call to wp_get_current_request_url(), which handles both the standard case and the reverse-proxy case (missing path prefix) correctly. See https://core.trac.wordpress.org/ticket/53998 Co-authored-by: Claude (Anthropic) <noreply@anthropic.com>
wp_referer_field() outputs a hidden form field containing the current request URL so that form handlers can redirect back to the originating page. It called remove_query_arg() without an explicit URL, which defaults to the raw $_SERVER['REQUEST_URI']. Behind a path-stripping reverse proxy, REQUEST_URI is missing the site's path prefix. For example on a subdirectory install served under /subdir/: Expected: /subdir/my-account/ Actual: /my-account/ After login, code that reads this value via wp_get_raw_referer() redirects the browser to /my-account/ — a path that does not exist on the proxy because the subdirectory prefix is missing. Fix: pass wp_get_current_request_url() as the base URL to remove_query_arg(). This produces a full absolute URL that includes the home path from the DB-configured site address, so the redirect works correctly in both standard installs and behind reverse proxies. See https://core.trac.wordpress.org/ticket/53998 Co-authored-by: Claude (Anthropic) <noreply@anthropic.com>
… call sites. Add unit tests covering the new wp_get_current_request_url() helper and the behavioral changes in wp_referer_field() and get_self_link(). New test files: - tests/phpunit/tests/link/wpGetCurrentRequestUrl.php 15 tests covering root installs, subdirectory installs, reverse-proxy prefix stripping, explicit $request_uri parameter, scheme detection, host resolution from home_url(), port preservation, and query strings. - tests/phpunit/tests/feed/getSelfLink.php 6 tests verifying get_self_link() delegates to wp_get_current_request_url() correctly across standard and reverse-proxy configurations. Updated test file: - tests/phpunit/tests/functions/wpRefererField.php Adjusted existing tests for the new absolute-URL behavior and added tests for subdirectory-aware referer values and query-arg stripping. See #53998.
… call sites. Add unit tests covering the new wp_get_current_request_url() helper and the behavioral changes in wp_referer_field() and get_self_link(). New test files: - tests/phpunit/tests/link/wpGetCurrentRequestUrl.php 15 tests covering root installs, subdirectory installs, reverse-proxy prefix stripping, explicit $request_uri parameter, scheme detection, host resolution from home_url(), port preservation, and query strings. - tests/phpunit/tests/feed/getSelfLink.php 6 tests verifying get_self_link() delegates to wp_get_current_request_url() correctly across standard and reverse-proxy configurations. Updated test file: - tests/phpunit/tests/functions/wpRefererField.php Adjusted existing tests for the new absolute-URL behavior and added tests for subdirectory-aware referer values and query-arg stripping. See #53998.
Replace manual URL construction from
$_SERVER['HTTP_HOST']withnetwork_home_url()Several places in WordPress core build absolute URLs by concatenating raw
$_SERVER['HTTP_HOST']with$_SERVER['REQUEST_URI']:This breaks when WordPress is behind a reverse proxy that rewrites paths —
HTTP_HOSTreflects the proxy's internal hostname andREQUEST_URImay have had a path prefix stripped. WordPress already providesnetwork_home_url()which reads the canonical URL from the database and handles scheme, host, and path prefix correctly. This PR replaces every URL-construction use ofHTTP_HOSTwith the appropriate WordPress URL function.Files changed (11 files, net −24 lines):
wp-admin/includes/class-wp-list-table.phpwp-admin/includes/misc.phpwp_admin_canonical_url()wp-includes/admin-bar.phpwp-includes/blocks/loginout.phpwp-includes/canonical.phpredirect_canonical()fallback URLwp-includes/class-wp-recovery-mode.php$schemevariable)wp-includes/functions.phpwp_auth_check_html()domain comparisonwp-includes/general-template.phpwp_login_form()default redirectwp-includes/nav-menu-template.phpwp-includes/pluggable.phpauth_redirect()— 3 SSL redirects + login redirect; alsowp_redirect()→wp_safe_redirect()wp-login.phpIntentionally unchanged
HTTP_HOSTusages:wp-includes/media.phpwp_calculate_image_srcset()— domain-identity check (not URL construction)wp-login.phpRELOCATEblock — deliberately reads the actual host to updatesiteurlTrac ticket: https://core.trac.wordpress.org/ticket/53998
Use of AI Tools
This PR was developed with the assistance of Cursor IDE with Claude. The AI was used for:
$_SERVER['HTTP_HOST']used for URL construction in core are consideredAll changes were reviewed, tested, and approved by the author. Most of these changes have already been done in 2022.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.