Redirects
When making a non-GET Inertia request manually or via a <Link> element, you should ensure that you always respond with a proper Inertia redirect response.
For example, if your controller or handler is creating a new user, your "store" endpoint should return a redirect back to a standard GET endpoint, such as your user "index" page. Inertia will automatically follow this redirect and update the page accordingly.
1class UsersController extends Controller2{3 public function index()4 {5 return Inertia::render('Users/Index', [6 'users' => User::all(),7 ]);8 }910 public function store(Request $request)11 {12 User::create(13 $request->validate([14 'name' => ['required', 'max:50'],15 'email' => ['required', 'max:50', 'email'],16 ])17 );1819 return to_route('users.index');20 }21}1class UsersController extends Controller2{3 public function index()4 {5 return Inertia::render('Users/Index', [6 'users' => User::all(),7 ]);8 }910 public function store(Request $request)11 {12 User::create(13 $request->validate([14 'name' => ['required', 'max:50'],15 'email' => ['required', 'max:50', 'email'],16 ])17 );1819 return to_route('users.index');20 }21}
1use laravel::inertia::{self, Page, props};2use laravel::model::create;3use laravel::prelude::*;45use crate::models::User;67#[laravel::input(fills = User)]8#[validate(anyone)]9pub struct NewUser {10 #[validate(required, max_length(50))]11 pub name: String,12 #[validate(required, max_length(50), email)]13 pub email: String,14}1516pub async fn index() -> laravel::Result<Page> {17 let users = User::all().await?;1819 Ok(inertia::render("Users/Index", props! { users }))20}2122pub async fn store(Valid(input): Valid<NewUser>) -> laravel::Result<Redirect> {23 create!(User { ..input }).await?;2425 Ok(redirect!(index))26}1use laravel::inertia::{self, Page, props};2use laravel::model::create;3use laravel::prelude::*;45use crate::models::User;67#[laravel::input(fills = User)]8#[validate(anyone)]9pub struct NewUser {10 #[validate(required, max_length(50))]11 pub name: String,12 #[validate(required, max_length(50), email)]13 pub email: String,14}1516pub async fn index() -> laravel::Result<Page> {17 let users = User::all().await?;1819 Ok(inertia::render("Users/Index", props! { users }))20}2122pub async fn store(Valid(input): Valid<NewUser>) -> laravel::Result<Redirect> {23 create!(User { ..input }).await?;2425 Ok(redirect!(index))26}
303 Response Code
You must use a 303 response code when redirecting after a PUT, PATCH, DELETE, or QUERY request. Otherwise, the browser will not issue the subsequent request as a GET request. A 303 redirect is very similar to a 302 redirect; however, the follow-up request is explicitly changed to a GET request.
If you're using one of our official server-side adapters, all redirects will automatically be converted to 303 redirects.
Preserving Fragments
Sometimes a user may visit a URL with a fragment, such as /article/old-slug#section, and the server needs to redirect to a different URL. The fragment from the original request is normally lost during the redirect.
1import { Link } from '@inertiajs/vue3'23<Link href="/article/old-slug#section">View section</Link>1import { Link } from '@inertiajs/vue3'23<Link href="/article/old-slug#section">View section</Link>
1import { Link } from "@inertiajs/react";23<Link href="/article/old-slug#section">View section</Link>1import { Link } from "@inertiajs/react";23<Link href="/article/old-slug#section">View section</Link>
1import { Link } from '@inertiajs/svelte'23<Link href="/article/old-slug#section">View section</Link>1import { Link } from '@inertiajs/svelte'23<Link href="/article/old-slug#section">View section</Link>
You may preserve the fragment on the redirect response. The client will carry over the #section fragment to the redirect target, resulting in /article/new-slug#section.
1return redirect('/article/new-slug')->preserveFragment();1return redirect('/article/new-slug')->preserveFragment();
1use laravel::inertia;2use laravel::prelude::*;34pub async fn show() -> laravel::Result<Redirect> {5 inertia::preserve_fragment()?;67 Ok(Redirect::to("/article/new-slug"))8}1use laravel::inertia;2use laravel::prelude::*;34pub async fn show() -> laravel::Result<Redirect> {5 inertia::preserve_fragment()?;67 Ok(Redirect::to("/article/new-slug"))8}
External Redirects
Sometimes it's necessary to redirect to an external website, or even another non-Inertia endpoint in your app while handling an Inertia request. This can be accomplished using a server-side initiated window.location visit via Inertia::location()inertia::location().
1return Inertia::location($url);1return Inertia::location($url);
1use laravel::inertia::{self, HttpResponse};23pub async fn redirect_external() -> laravel::Result<HttpResponse> {4 inertia::location("https://example.com")5}1use laravel::inertia::{self, HttpResponse};23pub async fn redirect_external() -> laravel::Result<HttpResponse> {4 inertia::location("https://example.com")5}
This generates a 409 Conflict response and includes the destination URL in the X-Inertia-Location header. When this response is received client-side, Inertia will automatically perform a window.location = url visit.
Previous URL
Laravel Adapter v3.4.0+
Sometimes you may wish to know which page the user navigated from. In Laravel PHP, Laravel's session middleware doesn't store the previous URL and route for Inertia visits, since they are sent as AJAX requests, so session()->previousUrl(), session()->previousUri(), and session()->previousRoute() return the last full page load. The back() helper is unaffected, as it resolves the previous location from the request's Referer header. You may enable the store_previous_url option in your config/inertia.php configuration file to store the previous location for Inertia visits as well.
1return [2 'store_previous_url' => true,3];1return [2 'store_previous_url' => true,3];
Prefetch requests and partial reloads are excluded, since deferred props, polling, and infinite scroll requests aren't navigations the user came from. Overriding the shouldStoreCurrentUrl() method in your HandleInertiaRequests middleware allows you to customize which visits are stored.
1use Illuminate\Http\Request;2use Inertia\Middleware;3use Symfony\Component\HttpFoundation\Response;45class HandleInertiaRequests extends Middleware6{7 public function shouldStoreCurrentUrl(Request $request, Response $response): bool8 {9 return parent::shouldStoreCurrentUrl($request, $response)10 && ! $request->routeIs('admin.*');11 }12}1use Illuminate\Http\Request;2use Inertia\Middleware;3use Symfony\Component\HttpFoundation\Response;45class HandleInertiaRequests extends Middleware6{7 public function shouldStoreCurrentUrl(Request $request, Response $response): bool8 {9 return parent::shouldStoreCurrentUrl($request, $response)10 && ! $request->routeIs('admin.*');11 }12}