What is the difference between CI4 redirect()->to() and redirect()->back()?

Why Interviewers Ask This

Advanced questions like this reveal whether a candidate has internalized CodeIgniter deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.

Answer

Both are redirect methods in CI4 but serve different purposes. redirect()->to($uri) redirects to a specific URL or URI. If a relative URI is given, base_url() is prepended. Status code: 302 by default; pass a second argument for other codes: redirect()->to("/home", 301). redirect()->back() redirects to the previous page using the HTTP Referer header. If no Referer is available, it redirects to the base URL. It is typically used after form submission failures. Both support fluent methods: ->with("key", "value") (flash to session), ->withInput() (flash current POST data for form repopulation), ->withCookies() (send response cookies with redirect). Named routes: redirect()->route("users.index"). The redirect response must be returned: return redirect()->to("/dashboard"). Do not use PHP's header("Location: ...") — it bypasses CI4's response pipeline.

Common Mistake

Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong CodeIgniter candidates.