簡體   English   中英

如何在 Next.js 配置中重定向 / 到 basePath?

[英]How to redirect / into basePath in Next.js config?

每次該用戶進入/我想重定向到我已經設置的basePath

這是我的next.config.js

module.exports = {
  basePath: '/docs',
}

所以每次我輸入路徑/我想重定向到/docs

這是我嘗試過的。

module.exports = {
  basePath: '/docs',
  async rewrites() {
    return [
      {
        source: '/',
        destination: '/docs',
      },
    ];
  },
  async redirects() {
    return [
      {
        source: '/',
        destination: '/docs',
      },
    ];
  },
};

問題是這種rewritesredirects僅適用於basePath

例如

async redirects() {
        return [
          {
            source: '/test',
            destination: '/hello',
          },
        ];
      },

我進入/docs/test它將重定向到/docs/hello

但我想//docs

exportPathMap允許您指定請求路徑到頁面目標的映射,以在導出期間使用。 使用 next dev 時,exportPathMap 中定義的路徑也將可用。

例子

 module.exports = { exportPathMap: async function ( defaultPathMap, { dev, dir, outDir, distDir, buildId } ) { return { '/': { page: '/' }, '/about': { page: '/about' }, '/p/hello-nextjs': { page: '/post', query: { title: 'hello-nextjs' } }, '/p/learn-nextjs': { page: '/post', query: { title: 'learn-nextjs' } }, '/p/deploy-nextjs': { page: '/post', query: { title: 'deploy-nextjs' } }, } }, }

您可以使用basePath: false選項來禁用sourcedestination的自動basePath前綴。

module.exports = {
    basePath: '/docs',
    async redirects() {
        return [
            {
                source: '/',
                destination: '/docs',
                basePath: false
            }
        ]
    }
};

這將正確地將/路徑重定向到/docs

如果您要使用它,這同樣適用於rewrites

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM