throw redirect()

// Redirect the user to another URL
redirect(url: `/${string}` | `https://${string}` | `http://${string}`)
// /pages/some-page/+someHook.js
 
import { redirect } from 'vike/abort'
 
export function someHook() {
  if (someCondition) {
    throw redirect('/some-url')
  }
}

throw redirect() is about aborting a page from being rendered and redirecting to the user to another page instead. Use navigate() if you want to redirect after the page is already rendered. See Abort > throw redirect() VS throw render() VS navigate().

throw redirect() makes temporary redirections (HTTP status code 302). For permanent redirections (HTTP status code 301), use the redirects setting or pass a second argument throw redirect('/some-url', 301).

Common use cases:

While it's most commonly used with guard() or data() you can use it with any hook.

For improved DX, consider using throw render() instead of throw redirect(). See Abort > throw redirect() VS throw render() VS navigate().

If throw redirect() doesn't work, see Abort > Debug and Abort > throw redirect() VS throw render() VS navigate().

See also