⚠
vite-plugin-ssr has been renamed Vike, see migration guide.

throw redirect()

See also:

// Redirect the user to another URL
redirect(url: `/${string}` | `https://${string}` | `http://${string}`)
import { redirect } from 'vite-plugin-ssr/abort'

function onSomeHook() {
  if (someCondition) {
    throw redirect('/some-url')
  }
}

throw redirect() makes temporary redirections (HTTP status code 302). For permanent redirections (HTTP status code 301), you can set config.redirects or pass a second argument throw redirect('/some-url', 301).

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

Common use cases:

If throw redirect() doesn't work, see Abort > Debug.