如何根据 IP 位置将用户重定向到子域?

How to redirect users to a subdomain based on IP location?

我正在为我的网站使用 Node Express,我想根据他们当前的位置重定向用户,例如,如果新加坡的用户打开 site.com,然后将他们重定向到子域 sg.site.com(这个与 site.com 托管在同一台服务器上)。我能够找到用户的 IP 地址和位置,但如何将它们重定向到正确的子域?

如果你的意思是来自服务器端 Express 代码,res.redirect:

res.redirect([status,] path)

Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code . If not specified, status defaults to “302 “Found”.

res.redirect('/foo/bar');
res.redirect('http://example.com');
res.redirect(301, 'http://example.com');
res.redirect('../login');

如果您指的是客户端,请分配给 location object

Replace the current document with the one at the given URL:

function goMoz() {   
   window.location = "http://www.mozilla.org";  
}   

你可以做到

res.redirect('https://app.example.io');

快递文件:https://expressjs.com/en/api.html#res.redirect


它的工作原理基本上就像您将用户重定向到外部页面一样,但您也可以添加您的子域。喜欢这个:

function requestHandler(req, res) {
    res.redirect('https://sg.site.com');
}