Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

spazzwig's avatar

Recommended approach to conditionally modify path of all requests?

Heyo, I'm trying to determine the best way to prepend a segment to all request URLs, at a stage of the request lifecycle that will affect all responses (e.g. mix/asset paths, generated urls).

My initial thought was a middleware, but I haven't found a good way to modify the path in the request object. I suspect it may not be considered a good practice to change the request like that anyway; this is the use case I'm looking to address:

App is installed to run from web root (per normal). Some requests come directly to the host URL at that path, but some requests are coming from a proxy and need a prepended segment to match the path the proxy is requesting from. There isn't an option to modify the proxy configuration. Right now I am handling this ok via a bit of reverse proxy config in nginx, though this doesn't address the path generation from within Laravel.

Essentially, looking for a better way to approach this that ticks the following boxes:

  • Preferably handled from the app layer (instead of host config, would like this to be more portable or able to run on Vapor, etc)
  • Conditionally prepends the path (the requesting proxy sends headers that allow for detection and provide the condition)
  • Modifies the path before anything in the app reads/uses it for a response

TL;DR What is a good way to satisfy the following request conditions from within the same app, where [proxy_path] is a persistent root path included in all requests from the proxy and needs to be handled and represented in any paths as part of the response?:

[app_host]/[request_path]
[proxy_host]/[proxy_path]/[request_path]

Hope this all makes sense!

0 likes
0 replies

Please or to participate in this conversation.