How can I integerate relative URL structure to work with absolute routing in Expressive?

I am trying to integrate absolute routing model with an existing website that uses relative URLs.

By absolute I mean a URL like /path/action/params/, note the slash in the front.
And by relative I mean a URL like controller.php?action=view&params, not the lack of slash.

One issue I ran into is say I am on a URL like so, which is my “before Expressive” URL:

  1. https://host/portal.php?p=sales&c=view&id=535

Inside the view template there is a JavaScript control to edit a comment box, and the edit link is like so:

  1. portal.php?p=db&mode=edit&field=comments&id=535

When clicked, link #2 loads an existing non-Expressive controller via AJAX to show an HTML edit box. #2 uses a relative URL, and since the original link was at the app root level, the control is also loaded at the root level. The rendered edit box has OK and CANCEL buttons, which themselves use relative URLs similar to that of #2. The edit control is used extensively throughout the application, so any changes to it need to be done with some care.

Add Expressive Routing

Then, I rewrote the View part in Expressive, and gave it a new link like so:

  1. http://host/sales/view/535

but the JavaScript control and other links it AJAX-loaded remained the same. So now, when I click the edit control, it fails to load, because it uses relative linking, and thus loads at the “new route level” of link #3, which is no longer at the app’s root. What I get specifically, is a merged URL #1 and #2, forming a route that does not exist:

  1. http://host/sales/view/portal.php?p=db&mode=edit&field=comments&id=535

How can I tackle this issue?

A simple fix does not always work

An easy fix could be to edit my “legacy” (non-Expressive) URLs to prepend a slash / to them, making them load from the app’s root. And that works, but to a point. Since URLs are relative, my DEV environment is configured to run the app from the actual HTTP Root, so prepending the slash does actually work on DEV. But on PROD, the app runs from a subfolder called portal, on top of the HTTP Root. So to make links work on PROD I have to prepend that subfolder to the URL, like so: /portal/. That creates a problem because URL prefixes have to be different in different environments.

One thing I could do is to define a $url_prefix that is different on DEV and on PROD and prepend those to each URL.

Another thing I could do is to reconfigure my DEV to run from the same subfolder as PROD, eliminating the need for different URL prefixes for each environment, but I will still need to update my legacy URLs to have the $url_prefix for them to work properly with Expressive routing.

Is there a better solution?