【问题标题】:How can i disable nuxt / redirect我如何禁用 nuxt / 重定向
【发布时间】:2021-04-16 09:30:30
【问题描述】:

我的 nuxt 应用程序重定向所有以 / 结尾的 url 例如:localhost/list/ 重定向到localhost/list

我可以禁用此重定向吗?

谢谢!

【问题讨论】:

  • 请分享您的nuxt.config.js 文件

标签: node.js nuxt.js


【解决方案1】:

您可以在 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

【讨论】:

  • trailingSlash 没有解决我的问题,但这个答案帮助我发现我在配置中重定向错误
猜你喜欢
  • 2020-04-05
  • 2021-06-05
  • 1970-01-01
  • 2021-06-21
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 2013-02-26
相关资源
最近更新 更多