如何檢測如果訪問者是通過手機登錄並打開他的index.php 從PC登錄時把他的index.html如果將訪問者切換到頁面,如果他通過手機登錄到網站,並且如果從PC登錄,則訪問另一個頁面?
是通過.htaccess進行還是什麼?
如何檢測如果訪問者是通過手機登錄並打開他的index.php 從PC登錄時把他的index.html如果將訪問者切換到頁面,如果他通過手機登錄到網站,並且如果從PC登錄,則訪問另一個頁面?
是通過.htaccess進行還是什麼?
您需要使用客戶端提供的某種信息來確定用戶是否在移動設備上。
有任何多種方式在JavaScript
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
/* User is mobile... */
}
這將檢查在用戶代理的移動設備使用的字符串來做到這一點,例如。
有像Modernizr(http://www.modernizr.com/)這樣的圖書館有這樣的事情來幫助你。
總而言之,沒有辦法做到這一點 - 你必須嘗試不同的方法,並選擇你喜歡的。
您可以在DOCUMENT_ROOT/.htaccess
文件中使用這些規則:
RewriteEngine On
RewriteBase/
# forward mobile users to index.php
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android"[NC]
RewriteRule^- [E=ISMOBILE:1]
RewriteCond %{ENV:ISMOBILE} =1
RewriteRule !^index\.php$ index.php [L]
# otherwise desktop users to index.html
RewriteCond %{ENV:ISMOBILE} !=1
RewriteRule !^index\.html$ index.html [L]
它可以用這個PHP腳本來完成,它會如果移動返回真,一些移動用戶代理缺失
<?php
// ------- DETECT USER DEVICE ----------
$user_device = "";
$IsMobile = "";
$agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match("/Valve/", $agent)) {
$user_device = "Steam GameOverlay";
} else if (preg_match("/Safari/", $agent)) {
$user_device = "Safari";
} else if (preg_match("/Android/", $agent)) {
$user_device = "Android Mobile";
} else if (preg_match("/IEMobile/", $agent)) {
$user_device = "Windows Mobile";
} else if (preg_match("/Chrome/", $agent)) {
$user_device = "Google Chrome";
} else if (preg_match("/MSIE/", $agent)) {
$user_device = "Internet Explorer";
} else if (preg_match("/Firefox/", $agent)) {
$user_device = "Firefox";
} else if (preg_match("/Opera/", $agent)) {
$user_device = "Opera";
}
$OSList = array
(
// Match user agent string with operating systems
'Android' => 'Android',
'Windows 3.11' => 'Win16',
'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
'Windows 98' => '(Windows 98)|(Win98)',
'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
'Windows Server 2003' => '(Windows NT 5.2)',
'Windows Vista' => '(Windows NT 6.0)',
'Windows Phone' => '(XBLWP7)|(ZuneWP7)|(Windows Phone OS 7.5)|(Windows Phone OS 7.0)|(Windows Phone 8.0)',
'Windows 8' => '(Windows NT 6.2)',
'Windows 7' => '(Windows NT 6.1)|(Windows NT 7.0)',
'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
'Windows ME' => 'Windows ME',
'Open BSD' => 'OpenBSD',
'Sun OS' => 'SunOS',
'Linux' => '(Linux)|(X11)',
'iPhone' => 'iPhone',
'iPad' => 'iPad',
'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
'QNX' => 'QNX',
'BeOS' => 'BeOS',
'OS/2' => 'OS/2',
'Mac OS' => 'Mac OS',
'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
);
// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match) {
// Find a match
if (@eregi($Match, $agent)) {
break;
} else {
$CurrOS = "Ukendt OS";
}
}
if ($user_device == ""){
$user_device = "Ukendt Browser";
}
//$device = "$user_device : $CurrOS";
$device = "$CurrOS";
// ------- END DETECT USER DEVICE ----------
if ($CurrOS == "Android" || $CurrOS == "Windows Phone" || $CurrOS == "iPhone"){
$IsMobile = "True ".$CurrOS;
}else{
$IsMobile = "False ".$CurrOS;
}
?>
我不會像你所做的那樣推薦使用'eregi'。 '這個函數從PHP 5.3.0開始已經被拒絕了。依賴於這個特性是非常令人沮喪的。http://uk1.php.net/eregi – Ryan
當我更新代碼到preg_match時,糟糕的是這個問題。 – AlexanderYW
通過'.htaccess',你可以按照這個線程:http://stackoverflow.com/questions/3680463/mobile-redirect-using-htaccess – MTranchant
你是專門要求htaccess解決方案,還是問題開放的替代技術?由於我的回答中有評論,我要問。 – Ryan