# 重定向和別名
## [#](https://router.vuejs.org/zh/guide/essentials/redirect-and-alias.html#重定向) 重定向
重定向也是通過 `routes` 配置來完成,下面例子是從 `/a` 重定向到 `/b`:
~~~
const router = new VueRouter({
routes: [
{ path: '/a', redirect: '/b' }
]
})
~~~
重定向的目標也可以是一個命名的路由:
~~~
const router = new VueRouter({
routes: [
{ path: '/a', redirect: { name: 'foo' }}
]
})
~~~
甚至是一個方法,動態返回重定向目標:
~~~
const router = new VueRouter({
routes: [
{ path: '/a', redirect: to => {
// 方法接收 目標路由 作為參數
// return 重定向的 字符串路徑/路徑對象
}}
]
})
~~~
注意[導航守衛](https://router.vuejs.org/zh/guide/advanced/navigation-guards.html)并沒有應用在跳轉路由上,而僅僅應用在其目標上。在下面這個例子中,為 `/a` 路由添加一個 `beforeEach` 或 `beforeLeave` 守衛并不會有任何效果。
其它高級用法,請參考[例子](https://github.com/vuejs/vue-router/blob/dev/examples/redirect/app.js)
[](https://github.com/vuejs/vue-router/blob/dev/examples/redirect/app.js)
。
## [#](https://router.vuejs.org/zh/guide/essentials/redirect-and-alias.html#別名) 別名
“重定向”的意思是,當用戶訪問 `/a`時,URL 將會被替換成 `/b`,然后匹配路由為 `/b`,那么“別名”又是什么呢?
**`/a` 的別名是 `/b`,意味著,當用戶訪問 `/b` 時,URL 會保持為 `/b`,但是路由匹配則為 `/a`,就像用戶訪問 `/a` 一樣。**
上面對應的路由配置為:
~~~
const router = new VueRouter({
routes: [
{ path: '/a', component: A, alias: '/b' }
]
})
~~~
“別名”的功能讓你可以自由地將 UI 結構映射到任意的 URL,而不是受限于配置的嵌套路由結構。
更多高級用法,請查看[例子](https://github.com/vuejs/vue-router/blob/dev/examples/route-alias/app.js)
[](https://github.com/vuejs/vue-router/blob/dev/examples/route-alias/app.js)。