Skip to main content
Page Redirection

Page Redirection

Redirects move the browser to another URL. They are commonly used after form submissions, during authentication, and when an old address should point to a new one.

Basic redirects

return redirect()->to('/dashboard');
return redirect()->to('products/42');
return redirect()->route('product', [42]);
return redirect()->back();

After a form submission

if (! $this->validate($rules)) {
    return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
}

return redirect()->to('/products')->with('success', 'Product created.');

This pattern is called post/redirect/get. It prevents a browser refresh from submitting the form again.

Status codes

return redirect()->to('/new-home', 301); // permanent move
return redirect()->to('/login', 302);     // temporary (default)

Use 301 only when the old URL is permanently gone and search engines should update their index.

Redirect from routes

Simple legacy URLs can be redirected in app/Config/Routes.php without a controller. Use a dedicated controller or route redirect and keep the target list small and documented.

Good practices

  • Always return the redirect from the controller.
  • Prefer named routes or site_url() over hard-coded absolute URLs.
  • Do not redirect in a loop; check that the target is not the same path.
  • Store messages in flashdata, not in the URL, so users can refresh safely.