nextresponse.redirect()`redirect状态代码 我将Next.js用于我的应用程序,出于SEO的目的,我正在使用URL中的Locales。 因此,在www.domain.com的响应中,我使用服务器端重定向将其重定向到具有语言环境的域(例如:www.domain.com ...

问题描述 投票:0回答:2
,我使用

Server侧重定向将其重定向到具有区域的域(例如:www.domain.com/en

。)
完成我使用Next.js中间件的任务,基于Next.js文档提出的this示例
import { NextMiddleware, NextRequest, NextResponse } from "next/server";

const PUBLIC_FILE = /\.(.*)$/;

export const middleware: NextMiddleware = (request: NextRequest) => {
  const shouldHandleLocale =
    !PUBLIC_FILE.test(request.nextUrl.pathname) &&
    !request.nextUrl.pathname.includes("/api/") &&
    request.nextUrl.locale === "default";

  if (shouldHandleLocale) {
    const url = request.nextUrl.clone();
    url.pathname = `/en${request.nextUrl.pathname}`;
    return NextResponse.redirect(url, 308);
  }

  return undefined;
};

在函数中,i返回了tossigng重定向类型的308状态代码永久。 (在您想了解重定向类型的情况下,请阅读文档

Here

它在应用程序上的所有页面上都可以正常工作,除了首页(

NextResponse.redirect()
),其中第一个对主域请求("/")用satus代码的307的响应,该代码代表了theuctirectiect

307

作为Google搜索中心的“弱信号”。
我仍然必须对哪个对SEO事项更好的研究进行一些研究,如果您能为此提供帮助。
但我的主要问题是,为什么状态代码分配不适用于主页链接。
感谢您的帮助
    
对我来说,这有效 www.domain.com

next。 但是,基于重定向函数的源代码(在Next.js Ver 15.0.2中,我正在使用[我认为此代码尚未从VER 13更改])

您可以使用此代码发送状态代码:

return NextResponse.redirect(url, { status: 308 });

或这个:
node.js http-redirect next.js seo
2个回答
5
投票
return NextResponse.redirect(url, { status: 308 });

这是源代码:
return NextResponse.redirect(url, { status: 308 });

0
投票

从我在接下来的类型定义中看到的内容,重定向签名如下:

static redirect(url, init) { const status = typeof init === 'number' ? init : (init == null ? void 0 : init.status) ?? 307; if (!REDIRECTS.has(status)) { throw new RangeError('Failed to execute "redirect" on "response": Invalid status code'); } const initObj = typeof init === 'object' ? init : {}; const headers = new Headers(initObj == null ? void 0 : initObj.headers); headers.set('Location', (0, _utils.validateURL)(url)); return new NextResponse(null, { ...initObj, headers, status }); }
where

static redirect(url: string | NextURL | URL, init?: number | ResponseInit): NextResponse;

包含
init
.

在重定向方法中不接受状态代码。
	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.