You can do it like this (in PHP) I don't code Laravel
$arr_url = parse_url('http://example.com/old_location?foo=bar');
$query = !empty($arr_url['query']) ? '?'.$arr_url['query'] : '';
header("Location: http://example.com/new_location{$query}");
exit;
OR you can use $_SERVER ( I forget if it includes the ?)
$query = !empty($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : '';
header("Location: http://example.com/new_location{$query}");
Two important things.
- no output before calling
header not so much as a single space
- call exit after using header.
from non-existant URL
As I said I don't use Laraval, so I can't speak on to how it handles 404's so you would need to do the redirection in the controller that handles that, which would require some logic to decide to redirect or not.
UPDATE
I want to do it in PHP, but this doesn't really help if my URL ending with image.png isn't a valid php file.
Saying access REST implies that it will be a PHP file. If you just mean finding if a file exists and redirecting then use HTACCESS
#if not is a real file
RewriteCond %{REQUEST_FILENAME} !-f
//redirect
How you decide where to redirect is an open question, as this is to broad for me to give a specific answer.
One last thing is your .png example. It's possible to use a .php file to return image data, with the correct headers. I once built a site that stored user images in a Zip file and then only displayed images if a user was logged in, by streaming the contents of the file from the ZIP (no temp files).
Everything the server returns is text, the only reason it's seen as an image is because Apache knows that .png should have Content-type: image/jpng or something like that for the content header. Something that can be "faked" in PHP.