【发布时间】:2021-04-16 09:30:30
【问题描述】:
我的 nuxt 应用程序重定向所有以 / 结尾的 url
例如:localhost/list/ 重定向到localhost/list
我可以禁用此重定向吗?
谢谢!
【问题讨论】:
-
请分享您的
nuxt.config.js文件
我的 nuxt 应用程序重定向所有以 / 结尾的 url
例如:localhost/list/ 重定向到localhost/list
我可以禁用此重定向吗?
谢谢!
【问题讨论】:
nuxt.config.js 文件
您可以在 Nuxt 应用的路由器中更改 trailingSlash 配置。
为此,请更改(如果不存在则添加)nuxt.config.js 文件,如下所示:
export default {
router: {
trailingSlash: false
},
}
如果您想将所有尾部斜杠重定向到无斜杠路由,请按照以下说明操作:
首先使用NPM安装@nuxtjs/redirect-module:
npm i @nuxtjs/redirect-module
将以下块添加到nuxt.config.js:
{
modules: [
'@nuxtjs/redirect-module'
],
redirect: [
{
// eslint-disable-next-line
from: '(?!^\/$|^\/[?].*$)(.*\/[?](.*)$|.*\/$)',
to: (from, req) => {
const base = req._parsedUrl.pathname.replace(/\/$/, '');
const search = req._parsedUrl.search;
return base + (search != null ? search : '');
},
]
}
示例:localhost/list/ 将重定向到 localhost/list
【讨论】: