[英]How to use Nextjs middleware to redirect users to sub-domain based on IP
我試圖弄清楚如何正確使用 NextJs 的中間件將用戶從特定國家路由到另一個 web 域,但我無法讓它正常工作。 以下是我的設置:
主域: https://www.example.com子域: https://ca.example.Z4D236D9A2DFEDAD4BEC0C05C0
src/middleware.js
import { NextResponse } from "next/server";
export function middleware(req) {
const { nextUrl: geo } = req;
const country = geo.country;
if (country === "CA") {
return NextResponse.redirect(new URL(`https://ca.example.com`));
}
}
以上不起作用,我感覺我可能誤解了中間件在我的用例中是如何工作的。 任何指針將不勝感激!
好的,我自己想通了。 我已經簡化了代碼,它現在可以工作了
import { NextResponse } from "next/server";
export function middleware(req) {
const country = req.geo.country;
if (country === "CA") {
return NextResponse.redirect("https://ca.example.com");
} else {
return;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.