Skip to content

WPURL: Preserve trailing-slash style for origin-only URLs#220

Merged
adamziel merged 1 commit intotrunkfrom
adamziel/wpurl-trailing-slash
Apr 2, 2026
Merged

WPURL: Preserve trailing-slash style for origin-only URLs#220
adamziel merged 1 commit intotrunkfrom
adamziel/wpurl-trailing-slash

Conversation

@adamziel
Copy link
Copy Markdown
Collaborator

@adamziel adamziel commented Apr 2, 2026

Summary

WPURL::replace_base_url() relies on the WHATWG URL parser, which normalises an empty path to /. When an origin-only URL like https://example.com (no trailing slash) went through replace_base_url(), its parsed pathname was /, and the existing trailing-slash logic explicitly skipped trimming when $from_pathname === '/'. The result gained an unwanted trailing slash (https://newsite.com/).

This fixes it by also checking the raw_url option string (when available) to detect the original trailing-slash style. If the raw URL didn't end with / but the WHATWG parser added one, we strip it from the output. This matters for values like siteurl and home during content migrations.

Test plan

  • New unit tests in WPURLTest covering origin-only URLs with and without trailing slash
  • New integration tests in RewriteUrlsTest for origin-only URLs in block attributes and HTML attributes
  • All 2298 existing DataLiberation tests pass
  • Linter passes

The WHATWG URL parser normalises an empty path to "/", so
"https://example.com" gets a pathname of "/". The existing
trailing-slash logic skipped trimming when the pathname was "/",
which meant origin-only URLs gained an unwanted trailing slash
after replace_base_url().

When the raw URL string is available in options, use it to detect
whether the original URL ended with "/" and strip the WHATWG-added
slash accordingly.
@adamziel adamziel merged commit 788e2ce into trunk Apr 2, 2026
22 checks passed
@sirreal sirreal deleted the adamziel/wpurl-trailing-slash branch April 8, 2026 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant