我的网站使用 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 specifiedstatus
, 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"; }
关于javascript - 如何根据IP位置将用户重定向到子域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51416038/