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 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.
using InertiaCore;
public class UsersController : Controller{ private readonly AppDbContext _context;
public UsersController(AppDbContext context) { _context = context; }
public IActionResult Index() { return Inertia.Render("Users/Index", new { Users = _context.Users.ToList(), }); }
[HttpPost] public async Task<IActionResult> Store([FromBody] CreateUserRequest request) { if (!ModelState.IsValid) { // Validation errors are forwarded to the response automatically. return Index(); }
_context.Users.Add(new User { Name = request.Name, Email = request.Email }); await _context.SaveChangesAsync();
return RedirectToAction("Index"); }}class UsersController extends Controller{ public function index() { return Inertia::render('Users/Index', [ 'users' => User::all(), ]); }
public function store(Request $request) { User::create( $request->validate([ 'name' => ['required', 'max:50'], 'email' => ['required', 'max:50', 'email'], ]) );
return to_route('users.index'); }}303 Response Code
When redirecting after a PUT, PATCH, or DELETE request, you must use a 303 response code, otherwise the subsequent request will not be treated 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.
import { Link } from '@inertiajs/vue3'
<Link href="/article/old-slug#section">View section</Link>import { Link } from "@inertiajs/react";
<Link href="/article/old-slug#section">View section</Link>import { Link } from '@inertiajs/svelte'
<Link href="/article/old-slug#section">View section</Link>You may preserve the fragment by chaining the preserveFragment method on the redirect response. The client will carry over the #section fragment to the redirect target, resulting in /article/new-slug#section.
// Call Inertia.PreserveFragment() before returning the redirect — the// middleware attaches the X-Inertia-Preserve-Fragment header so the// client carries the current URL fragment over to the redirect target.Inertia.PreserveFragment();return Redirect("/article/new-slug");return redirect('/article/new-slug')->preserveFragment();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 the Inertia::location() method.
return Inertia.Location(url);return Inertia::location($url);The Inertia::location() method will generate a 409 Conflict response and include 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.