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

cleber's avatar

How to forward a request with parameters/arguments to a different URL?

Hello guys,

I am working on a system that receives a request to a URL and forwards the request to a different URL after performing some actions (not relevant for the question). Example: The system receives a GET request to http://localhost/users/123/my/path?foo=1&bar=abc and then forward the user to http://localhost/my/path?foo=1&bar=abc using GET as well. Getting the new path and arguments is not a problem.

Using redirects works perfectly when there are no arguments. However, I need to keep all the request data, like method name (GET, POST, etc) and arguments, which does not work with the redirect($newRoute). According to Laravel documentation and some experiments I tried, redirect($newRoute)->whith($params) works only for URL parameters, like http://localhost/{$id}/some/path, but not for http://localhost/some/path?id=456, for example.

I also tried creating a new request based in the old one. It works fine except for one essential detail: the URL in the browser remains the old one. Exemple: when requesting http://localhost/users/123/my/path?foo=1&bar=abc, the system behaves as expected but stays in the same URL, not the right one (http://localhost/my/path?foo=1&bar=abc). It is a requirement that the system return/go to the new URL. I create the new request like this, as in here:

    $newRequest = Request::create("/" . $newRoute, $originalRequest->getMethod(), $originalRequest->all());
    return Route::dispatch($newRequest);

Any thoughts on how can I accomplish what I need?

Thanks guys!

0 likes
2 replies
tisuchi's avatar

First of all, if you have to perform some other task, the redirect is not an appropriate choice here.

after performing some actions

Maybe, you can simply get GET value from URL, then perform whatever you want and redirect to your expected url.

In this way, at least you can do whatever you want after getting the value from url and then pass that url value to either get or post method.

cleber's avatar

Hi guys,

@tisuchi, I ended up by finding a solution using redirect, although you did not recommend it. Thank you for your answer and support, though.

For those interested in the solution I used for this, see this question on Stack Overflow.

Thanks.

Please or to participate in this conversation.