Redirects
Standard redirects
Use Yii's regular redirects after form submissions. They work for Inertia visits without any change:
public function actionStore(): Response
{
// ... save the model
return $this->redirect(['user/index']);
}Behind the scenes the adapter adjusts redirects for Inertia (XHR) requests:
- Yii's AJAX redirect. For AJAX requests
Response::redirect()replaces theLocationheader withX-Redirect, which an XHR cannot follow. The adapter restores theLocationheader. 303 See Other. A302redirect after aPUT,PATCHorDELETErequest becomes303, so the browser follows it with aGETinstead of repeating the original method.
Redirecting back
Inertia::back() redirects to the previous page (the Referer header), falling back to the home page or a given URL. It uses 303 after PUT, PATCH and DELETE:
return Inertia::back();
return Inertia::back(['user/index']); // fallback when there is no referrerAn action that returns nothing for an Inertia request also redirects back.
External redirects
A redirect to a page outside the Inertia application — another site, a payment provider, a non-Inertia page of your application — must be a full page visit. Use Inertia::location():
return Inertia::location('https://checkout.example.com/session/42');
return Inertia::location(['site/legacy-report']);For Inertia requests it responds with 409 Conflict and an X-Inertia-Location header, and the client navigates with window.location. For regular requests it is a normal 302 redirect.
Redirects with a fragment
An XHR cannot see the #fragment of a redirect target. When a redirect for an Inertia request points at a URL with a fragment, the adapter responds with 409 Conflict and an X-Inertia-Redirect header, and the client visits the URL itself, keeping the fragment:
return $this->redirect(['post/view', 'id' => $post->id, '#' => 'comment-' . $comment->id]);Prefetch requests are not converted.
To keep the fragment of the original visit across an ordinary redirect (e.g. /docs#installation redirecting to /docs/v2), call Inertia::preserveFragment() before redirecting.