Page MenuHomePhabricator

[SPIKE] Validate index.php Apache Behavior for canonical API Routing
Open, In Progress, MediumPublic

Description

Description

Within the PP3.5 workstream, we would like to update the routing for Wikimedia APIs to go through a canonical {projectHost}/api/* pathway. We want to confirm whether MediaWiki's existing index.php rewrite pattern in Apache can be reused for API routing, since this would let us support a canonical /api/ base URL without pushing all rerouting logic into the REST Gateway, and ensuring that internal requests that circumvent the REST Gateway are also handled correctly.

Changing the base URL isn't a simple string swap. The concept of the /api/ routing will be checked at the Gateway, yet MediaWiki generates and responds to direct requests on its own, meaning that any changes to the baseUrl may have cascading impacts, particularly for internal calls that do not go through the REST Gateway. MediaWiki also generates links to other endpoints within its own responses (eg: pagination, related resources), so every module needs to know which canonical URL prefix to use when it builds those links, and be able to respond correctly to requests with either /rest|api.php/ or /api/ baseUrls.

In a similar vein, MediaWiki cannot fully resolve page titles on its own, particularly if they have changed. Apache handles that rewrite before a request ever reaches index.php for remapping page titles, and the recommendation is to mimic that same pattern for /api/ → rest.php. The purpose of this ticket is to investigate the viability of that approach and make a recommendation for how to proceed with canonical API routing.

Conditions of acceptance

  • Document how index.php's Apache-level rewrite currently works
  • Determine if we can handle an /api|rest.php/ --> /api/ redirect through Apache, similar to how index.php works for page titles.
  • Identify what breaks (if anything) for internal callers that bypass the REST Gateway
    • Recommendation: does the new prefix need global MediaWiki awareness, or can it be scoped per module?
  • Update excalidraw (or other) diagram as needed to represent the design moving forward
  • Update PP3.5 team about findings, so that we can decide how to proceed with the remaining canonical routing work
  • [Optional] Grab time with Tim S for background on how the current API framework evolved, to make sure canonical routing decisions account for historical constraints that may not be obvious from current documentation. --> Let Halley know if you want to set up and/or participate in this

Implementation details

image.png (2,176×1,926 px, 401 KB)

Event Timeline

KineticPelagic subscribed.

As part of my Clinic Duty rotation, I am moving this task because it looks beneficial and interesting to discuss as a team in Tech Deep Dive.

From what I've gathered:

  • Apache has a RewriteRule for /wiki(/.*)? to send requests to /w/index.php
  • MediaWiki then extracts the page title from that request using $wgArticlePath see documentation

My thinking is we do the same thing more or less:

  • Rewrite /api(/.*)? to /w/rest.php or another similar php handler endpoint in Apache
  • Introduce a $wgRestApiPath or similar
  • Handle routing to the correct API modules in PHP there

Someone with more knowledge of that part of MediaWiki internals should poke a bunch of holes in these assumptions and then we can probably figure out a way to make something similarly shaped work.

Per conversation with Daniel:

  • $wgRestPath already exists, so we should be good to go there
  • Route handling should also exist there. In theory, it should 'just work' once we have the Apache rewrite, but we have never tested this with an Apache rewrite. We will need to test/emulate that locally.

So I think it sounds like the only missing piece is the RewriteRule?

NOTE: We assume that MediaWiki only sees the rewrite target, and wouldn't be aware that the original path is /api/ vs w/rest.php/ -- because of this, the returned paths will always be the canonical /api/ path. That should also not be a problem as long as we have the Apache routing. Need to double check, but if that's the case, then it should be okay, but there may be some hidden madness there.

@Clement_Goubert -- we're potentially leaning towards just doing the testing live, since the new routes don't exist in the Gateway anyway. Do you see any risk with that? Should we start with local config first?

I think I found a way to test this locally without too much trouble, I still need to find time to fiddle with it though. But I also think that testing it "live" (internal to our network) should be fine.

@Clement_Goubert -- we're potentially leaning towards just doing the testing live, since the new routes don't exist in the Gateway anyway. Do you see any risk with that? Should we start with local config first?

We could probably find a way to test it in mw-experimental

Change #1329607 had a related patch set uploaded (by Clément Goubert; author: Clément Goubert):

[operations/puppet@production] mediawiki-vhost: Redirect /api/ to /w/rest.php

https://gerrit.wikimedia.org/r/1329607

I confirmed locally that rest routing works fine when /api/ gets rewritten to /w/rest.php by apache. Redirects use the path defined in $wgRestPath.

Change #1330505 had a related patch set uploaded (by Clément Goubert; author: Clément Goubert):

[operations/deployment-charts@master] mediawiki: Redirect /api/ to /w/rest.php

https://gerrit.wikimedia.org/r/1330505

Ftr, Clement and I confirmed on mw-experimental that the approach using the Apache rewrite will work.

@daniel @HCoplin-WMF for the deployment of those two patches, Clement is out sick, and with the end of quarter it's difficult to find a new person to take it over, so that may need to wait another week.

Please ping me if this becomes urgent

Change #1329607 merged by Clément Goubert:

[operations/puppet@production] mediawiki-vhost: Redirect /api/ to /w/rest.php

https://gerrit.wikimedia.org/r/1329607

Change #1330505 merged by jenkins-bot:

[operations/deployment-charts@master] mediawiki: Redirect /api/ to /w/rest.php

https://gerrit.wikimedia.org/r/1330505

Mentioned in SAL (#wikimedia-operations) [2026-09-30T10:56:21Z] <cgoubert@deploy1003> Started scap sync-world: 1330505: mediawiki: Redirect /api/ to /w/rest.php | T433547

Mentioned in SAL (#wikimedia-operations) [2026-09-30T10:59:34Z] <cgoubert@deploy1003> cgoubert: 1330505: mediawiki: Redirect /api/ to /w/rest.php | T433547 synced to the testservers (see https://wikitech.wikimedia.org/wiki/Mwdebug). Changes can now be verified there.

Apache patch is deployed and is ready for wgRestPath to be changed when needed

Clement_Goubert changed the task status from Open to In Progress.Wed, Sep 30, 11:04 AM
Clement_Goubert triaged this task as Medium priority.
Clement_Goubert moved this task from Scheduled (this Q) to In Progress on the ServiceOps board.
Clement_Goubert updated the task description. (Show Details)

Mentioned in SAL (#wikimedia-operations) [2026-09-30T11:07:08Z] <cgoubert@deploy1003> Finished scap sync-world: 1330505: mediawiki: Redirect /api/ to /w/rest.php | T433547 (duration: 11m 12s)

Apache patch is deployed and is ready for wgRestPath to be changed when needed

I can make a patch for that, but since the Gateway doesn't route the path, and afaik ATS doesn't either, I don't know hoe to test it...

Change #1352093 had a related patch set uploaded (by Daniel Kinzler; author: Daniel Kinzler):

[operations/mediawiki-config@master] REST: enable /api on testwiki

https://gerrit.wikimedia.org/r/1352093

For the API Gateway, we need to decide if we want to duplicate all 36 rules that we have there for /w/rest.php. At least 20 of them could be covered by a single rule. That change would only affect how the routes show up in Envoy's telemetry. I don't know how that's consumed, or who relies on it.

Change #1354040 had a related patch set uploaded (by Daniel Kinzler; author: Daniel Kinzler):

[operations/deployment-charts@master] DRAFT: REST gateway: add support for /api/ routes

https://gerrit.wikimedia.org/r/1354040