> ## Documentation Index
> Fetch the complete documentation index at: https://inertiajs-vi.tuantq.online/llms.txt
> Use this file to discover all available pages before exploring further.

# Redirect

Khi thực hiện request Inertia không phải `GET` theo cách thủ công hoặc thông qua phần tử `<Link>`, bạn cần đảm bảo luôn phản hồi bằng một response redirect Inertia hợp lệ.

Ví dụ, nếu controller đang tạo người dùng mới, endpoint "store" nên trả về redirect về một endpoint `GET` thông thường, chẳng hạn trang "index" người dùng. Inertia sẽ tự động đi theo redirect này và cập nhật trang tương ứng.

```php theme={null}
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');
    }
}
```

## Mã response 303

Khi redirect sau request `PUT`, `PATCH` hoặc `DELETE`, bạn phải dùng response code `303`, nếu không request tiếp theo sẽ không được xử lý như `GET`. Redirect `303` rất giống `302`; tuy nhiên follow-up request được chuyển rõ ràng thành `GET`.

Nếu đang dùng một trong các adapter phía máy chủ chính thức của Inertia, mọi redirect sẽ tự động được chuyển thành redirect `303`.

## Giữ fragment

Đôi khi người dùng truy cập URL có fragment, chẳng hạn `/article/old-slug#section`, và server cần redirect sang URL khác. Fragment của request ban đầu thường bị mất trong redirect.

<CodeGroup>
  ```vue Vue icon="vuejs" theme={null}
  import { Link } from '@inertiajs/vue3'

  <Link href="/article/old-slug#section">View section</Link>
  ```

  ```jsx React icon="react" theme={null}
  import { Link } from "@inertiajs/react";

  <Link href="/article/old-slug#section">View section</Link>
  ```

  ```svelte Svelte icon="s" theme={null}
  import { Link } from '@inertiajs/svelte'

  <Link href="/article/old-slug#section">View section</Link>
  ```
</CodeGroup>

Bạn có thể giữ fragment bằng cách nối phương thức `preserveFragment` trên redirect response. Client sẽ mang fragment `#section` sang redirect target, tạo thành `/article/new-slug#section`.

```php theme={null}
return redirect('/article/new-slug')->preserveFragment();
```

## Redirect ra bên ngoài

Đôi khi, trong lúc xử lý request Inertia, bạn cần redirect đến một website bên ngoài hoặc thậm chí một endpoint không dùng Inertia khác trong ứng dụng. Bạn có thể thực hiện việc này bằng một lần truy cập `window.location` được khởi tạo từ phía máy chủ thông qua phương thức `Inertia::location()`.

```php theme={null}
return Inertia::location($url);
```

Phương thức `Inertia::location()` sẽ tạo response `409 Conflict` và đặt URL đích trong header `X-Inertia-Location`. Khi phía client nhận được response này, Inertia sẽ tự động thực hiện truy cập bằng `window.location = url`.

***

## Tài liệu chính thức

Bài dịch này được đối chiếu từ [tài liệu Inertia.js v3 chính thức](https://inertiajs.com/docs/v3/the-basics/redirects). Nếu có khác biệt do phiên bản hoặc cập nhật mới, hãy ưu tiên tài liệu chính thức làm nguồn tham chiếu.
