[英]Redirect to a SubDomain
我正在使用 Yellowtree 的 GEOIP-Detect 插件,以便根據訪問者的位置將訪問者重定向到頁面。 但是我無法讓代碼完全工作。 該代碼首先獲取用戶 IP,然后將來自 IP 的信息存儲在多維數組中。 變量 $statecode 取出列出的狀態,然后根據取出的狀態數組調用 switch。
然后我轉到 Javascript 以嘗試將訪問者從當前頁面重定向到新頁面。 腳本通過 PHP 變量遷移,以便它可以在 JS 中正確讀取,然后一個函數嘗試使用 window.location.replace 重定向頁面,我讀到的內容僅適用於 Chrome,然后作為故障安全 window.location 用於其它瀏覽器。 然而,什么也沒有發生。 我哪里做錯了?
<?php
if (function_exists('geoip_detect2_get_info_from_current_ip')){
$userInfo = geoip_detect2_get_info_from_current_ip();
$stateCode = $userInfo->subdivisions->isoCode;
switch ($stateCode) {
case 'AL':
$url = 'subdomain1.domain.tld';
break;
case 'AR':
$url = 'subdomain2.domain.tld';
break;
default:
$url = 'subdomain3.domain.tld';
}
if ($url) { ?>
<script type="text/javascript">
var url = <?php echo $url ?>;
function(){
try { window.location.replace(url); }
catch(e) { window.location = url; }
}
</script>
<?php
}
}
?>
您可以通過發送Location
標頭直接在 PHP 中進行重定向。 請注意,您需要在$url
前面加上http://
,這樣它就不會被視為當前 URL 的相對路徑。
if ($url) {
header("Location: http://$url");
exit;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.