{
  "id": "8f37cd806e69ca3c55582fb3",
  "source": "solid:signals",
  "type": "github-document",
  "title": "redirect",
  "content": "---\ntitle: redirect\nuse_cases: \u003e-\n  authentication flows, login redirects, form submissions, route guards, user\n  authorization, navigation control\ntags:\n  - redirect\n  - authentication\n  - navigation\n  - routing\n  - authorization\n  - forms\nversion: \"1.0\"\ndescription: \u003e-\n  Redirect users between routes with proper status codes. Handle authentication\n  flows, form submissions, and protected route access.\n---\n\nThe `redirect` function returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object that instructs the router to navigate to a different route when returned or thrown from a [query](/solid-router/reference/data-apis/query) or [action](/solid-router/concepts/actions).\n\nThis works both in client and server (e.g., using a server function) environments.\n\n## Import\n\n```ts\nimport { redirect } from \"@solidjs/router\";\n```\n\n## Type\n\n```ts\nfunction redirect(\n\turl: string,\n\tinit?:\n\t\t| number\n\t\t| {\n\t\t\t\trevalidate?: string | string[];\n\t\t\t\theaders?: HeadersInit;\n\t\t\t\tstatus?: number;\n\t\t\t\tstatusText?: string;\n\t\t  }\n): CustomResponse\u003cnever\u003e;\n```\n\n## Parameters\n\n### `url`\n\n- **Type:** `string`\n- **Required:** Yes\n\nThe absolute or relative URL to which the redirect should occur.\n\n### `init`\n\n- **Type:** `number | { revalidate?: string | string[]; headers?: HeadersInit; status?: number; statusText?: string; }`\n- **Required:** No\n\nEither a number representing the status code or a configuration object with the following properties:\n\n#### `revalidate`\n\n- **Type:** `string | string[]`\n- **Required:** No\n\nA query key or an array of query keys to revalidate on the destination route.\n\n#### `status`\n\n- **Type:** `number`\n- **Required:** No\n\nThe HTTP status code for the redirect.\nDefaults to [`302 Found`)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/302).\n\n## Examples\n\n### Basic Usage\n\n```ts\nimport { query, redirect } from \"@solidjs/router\";\n\nconst getCurrentUserQuery = query(async () =\u003e {\n\tconst response = await fetch(\"/api/me\");\n\n\tif (response.status === 401) {\n\t\treturn redirect(\"/login\");\n\t}\n\n\treturn await response.json();\n}, \"currentUser\");\n```\n\n### Configuring Query Revalidation\n\n```ts\nimport { action, redirect } from \"@solidjs/router\";\n\nconst loginAction = action(async (formData: FormData) =\u003e {\n\tconst username = formData.get(\"username\")?.toString();\n\tconst password = formData.get(\"password\")?.toString();\n\n\tawait fetch(\"/api/login\", {\n\t\tmethod: \"POST\",\n\t\tbody: JSON.stringify({ username, password }),\n\t}).then((response) =\u003e response.json());\n\n\treturn redirect(\"/users\", { revalidate: [\"currentUser\"] });\n}, \"login\");\n```\n\n## Related\n\n- [`query`](/solid-router/reference/data-apis/query)\n- [`action`](/solid-router/reference/data-apis/action)",
  "url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/solid-router/reference/response-helpers/redirect.mdx",
  "metadata": {
    "path": "src/routes/solid-router/reference/response-helpers/redirect.mdx",
    "repo": "solidjs/solid-docs",
    "repo_url": "https://github.com/solidjs/solid-docs.git",
    "size": 2716,
    "source_type": "github"
  },
  "hash": "31d0fb87e921813d0fa8c49efef1d9acde4b51fe64d9b4bf48fbfae5db48fb8b",
  "timestamp": "2026-02-23T11:43:00.193359076+01:00"
}