最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to redirectinto basePath in Next.js config? - Stack Overflow

programmeradmin0浏览0评论

Every time that user enter into / I want to redirect into basePath that I already set.

Here is my next.config.js

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

So every time I enter path / I want to redirect into /docs.

Here is what I've tried.

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

The problem is this rewrites and redirects are working with basePath only.

For example

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

I enter into /docs/test it will redirect into /docs/hello.

But I want / to go to /docs.

Every time that user enter into / I want to redirect into basePath that I already set.

Here is my next.config.js

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

So every time I enter path / I want to redirect into /docs.

Here is what I've tried.

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

The problem is this rewrites and redirects are working with basePath only.

For example

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

I enter into /docs/test it will redirect into /docs/hello.

But I want / to go to /docs.

Share Improve this question edited Oct 19, 2021 at 8:33 juliomalves 50.3k23 gold badges177 silver badges168 bronze badges asked Oct 19, 2021 at 6:59 user8663822user8663822 2671 gold badge5 silver badges17 bronze badges 1
  • Conventionally you could also make use of routers to redirect to the desired path when a user reaches / (default index) path. – Prajwal Kulkarni Commented Oct 19, 2021 at 8:03
Add a comment  | 

1 Answer 1

Reset to default 17

You can use basePath: false option to disable the automatic basePath prefixing of source and destination.

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

This will correctly redirect / path to /docs.

The same applies to rewrites, if you were to use that instead.

发布评论

评论列表(0)

  1. 暂无评论