NextJS - 将所有重定向从 308 更改为 301

问题描述 投票:0回答:2

客户 SEO 要求将所有页面从 308 重定向到 301。项目是在 NextJS 13 中开发的,带有页面文件夹。我已经使用 middleware.ts 处理了大多数情况(迁移后的旧页面到新页面)

return NextResponse.redirect(new URL(address.destination), { status: 301 });

一般页面结构如下:xyz.com/offer/:brand/:model/

问题是,当用户输入

xyz.com//offer
xyz.com///offer
时,它会重定向到带有 308 代码的
xyz.com/offer

我还尝试在下一个配置中使用重定向:

        return [
            {
                source: '/offer/',
                destination: '/offer',
                statusCode: 301,
            },
        ];
    }

但是不可能用多个斜杠来预测所有情况+它不能按预期工作(不会发生 301 重定向)。

接下来有什么方法可以将所有内部重定向设置为 301 吗?

redirect next.js next.js13 http-status-code-301
2个回答
0
投票

更新您的 next.config.js 以包含用于处理重定向的自定义服务器重写规则:

// next.config.js

module.exports = {
  async rewrites() {
    return [
      // Redirect all routes with multiple slashes to the clean version with 301 status code
      {
        source: '/:path*',
        destination: '/:path*',
        permanent: true,
      },
    ];
  },
};

0
投票

如果你想停止 308 重定向,那么你可以在 next.config.ts/mjs/js 文件中禁用 2 个选项

const nextConfig = {
  trailingSlash: false, // MAKE THIS FALSE because this redirect is on 308.
  images: { unoptimized: true },
  reactStrictMode: false,
  cacheMaxMemorySize: 0
  permanent: false, // MAKE THIS OPTION FALSE. FOR REDIRECT
}
© www.soinside.com 2019 - 2024. All rights reserved.