How to redirect to external URL in Laravel with/without Inertia
Regular Laravel:
return redirect()->away("https://foo.com");
questionWhat’s the difference with regular
redirect()
method? From the Laravel docs:redirect()->away()
“creates aRedirectResponse
without any additional URL encoding, validation, or verification”. Both methods work, but it’s more efficient to useaway
(ie. no need for additional URL encoding etc) if we know it is an external URL.
Laravel + Inertia:
use Illuminate\Http\Request; // or class that extends it
use Inertia\Inertia;
public function foo(Request $request) {
$redirectUrl = "https://foo.com";
// ...
if ($request->inertia()) {
return Inertia::location($redirectUrl);
}
return redirect()->away($redirectUrl);
}
Inertia documentation: https://inertiajs.com/redirects
In: Laravel MOC