Skip to content

Rewrite Rules: Require a path-segment boundary when stripping the home path in WP::parse_request() - #13373

Open
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:fix/40339-home-path-regex-prefix-boundary
Open

Rewrite Rules: Require a path-segment boundary when stripping the home path in WP::parse_request()#13373
irozum wants to merge 2 commits into
WordPress:trunkfrom
irozum:fix/40339-home-path-regex-prefix-boundary

Conversation

@irozum

@irozum irozum commented Sep 2, 2026

Copy link
Copy Markdown

WP::parse_request() strips the site's home path off the front of PATH_INFO/REQUEST_URI/PHP_SELF using a regex built with sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ). Because this only anchors on the start of the string and never checks what comes right after the home path, it matches any path that merely begins with the same characters as the home path, not just paths that are actually nested under it.

For a site installed in a subdirectory named wp, a request for wp-json/wc/v1/products (a REST API route) gets the literal substring wp stripped off the front, leaving the mangled path -json/wc/v1/products — breaking REST API routing on such installs. The same root cause affects any endpoint whose name happens to share a prefix with the install's subdirectory (e.g. a sitemap.xml request on a site installed under /site).

This changes the regex to require a / or end-of-string right after the home path (!^%s(?:/|$)!i) before stripping it, so a bare prefix match like wp-json is left alone, while a real subdirectory match like wp/wp-json/... still has wp/ stripped correctly.

Adds a regression test, Tests_WP_ParseRequest::test_pathinfo_prefix_matching_home_path_is_not_stripped, which fails on trunk and passes with this fix.

Trac ticket: https://core.trac.wordpress.org/ticket/40339

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Implementation, tests, and PR description. Reviewed by Igor Rozum.


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.

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props irozum.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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