使用 htaccess 进行移动重定向
Posted
技术标签:
【中文标题】使用 htaccess 进行移动重定向【英文标题】:Mobile Redirect using htaccess 【发布时间】:2011-04-10 11:26:09 【问题描述】:我有一个网站叫
www.website.org
我有一个名为
的移动网站m.website.org
我想使用 htaccess 将主网站 URL 自动重定向到移动版本..
但是,移动版上有一个链接指向主网站,称为
www.website.org?noredirect=true
当我点击它链接到的实际网站主页上的徽标时
www.website.org
我不希望用户通过单击主页上的徽标意外返回移动设备。如何在没有 javascript 的情况下通过 htaccess 完成此操作。
如果不是,我对其他选择持开放态度。
编辑
我想我目前打算用它来通过 htaccess 感知移动重定向
RewriteEngine On
RewriteBase /
RewriteCond %HTTP_USER_AGENT android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ (ce|phone)|xda|xiino [NC,OR]
RewriteCond %HTTP_USER_AGENT ^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\ wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r\ |s\ )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1\ u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp(\ i|ip)|hs\-c|ht(c(\-|\ |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac(\ |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt(\ |\/)|klon|kpt\ |kwc\-|kyo(c|k)|le(no|xi)|lg(\ g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-|\ |o|v)|zz)|mt(50|p1|v\ )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v\ )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-|\ )|webc|whit|wi(g\ |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-) [NC]
RewriteRule ^$ http://m.website.com [R,L]
【问题讨论】:
你想如何预先确定他们应该被重定向到移动网站/如果他们点击回到主网站,你将如何在请求之间保持 noredirect 状态? 需要检查他们是否在手机上,我已经设置了一些 JavaScript 来更改 CSS,但如果他们试图离开,它将重定向回来。 您用于感知移动重定向的最后一次编辑存在一些错误。它有时会将 W7 上的 IE8 检测为移动设备。我目前无法重现此内容,但我会在进行更多调查后立即通知您。 【参考方案1】:Tim Stone 的解决方案是在正确的轨道上,但他的初始重写和他在最终条件下的 cookie 名称不同,你不能在同一个请求中写入和读取一个 cookie。
这是最终的工作代码:
RewriteEngine on
RewriteBase /
# Check if this is the noredirect query string
RewriteCond %QUERY_STRING (^|&)m=0(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.website.com]
# Check if this looks like a mobile device
# (You could add another [OR] to the second one and add in what you
# had to check, but I believe most mobile devices should send at
# least one of these headers)
RewriteCond %HTTP:x-wap-profile !^$ [OR]
RewriteCond %HTTP:Profile !^$ [OR]
RewriteCond %HTTP_USER_AGENT "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %HTTP_USER_AGENT "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %HTTP_USER_AGENT "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %HTTP_USER_AGENT "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %HTTP_USER_AGENT "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %HTTP_USER_AGENT "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %HTTP_USER_AGENT "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %HTTP_USER_AGENT "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %HTTP_USER_AGENT "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %HTTP_USER_AGENT !macintosh [NC]
# Check if we're not already on the mobile site
RewriteCond %HTTP_HOST !^m\.
# Can not read and write cookie in same request, must duplicate condition
RewriteCond %QUERY_STRING !(^|&)m=0(&|$)
# Check to make sure we haven't set the cookie before
RewriteCond %HTTP_COOKIE !^.*mredir=0.*$ [NC]
# Now redirect to the mobile site
RewriteRule ^ http://m.website.com [R,L]
【讨论】:
谢谢。一直在寻找重定向循环解决方案。到目前为止,这个问题解决得很好。 这会在 ipad 上显示桌面版还是移动版?如果是移动设备,默认情况下如何显示桌面版本?你有什么建议来了解更多关于这些正则表达式的信息吗? 这是一个很好的解决方案,但目前不适用于移动 Firefox/Chrome,但内部 Android 浏览器可以正常工作。我猜想添加更多的用户代理。可能我们需要类似:“android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile” 我在 2010 年发表了这篇文章,用户代理字符串肯定需要更新。 @J.T【参考方案2】:我测试了以下部分内容,但没有测试完整的规则集,所以如果您遇到问题,请告诉我,我会进一步挖掘。但是,假设我一切都正确,您可以尝试以下操作:
RewriteEngine On
# Check if this is the noredirect query string
RewriteCond %QUERY_STRING (^|&)noredirect=true(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%HTTP_HOST,S]
# Check if this looks like a mobile device
# (You could add another [OR] to the second one and add in what you
# had to check, but I believe most mobile devices should send at
# least one of these headers)
RewriteCond %HTTP:x-wap-profile !^$ [OR]
RewriteCond %HTTP:Profile !^$
# Check if we're not already on the mobile site
RewriteCond %HTTP_HOST !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %HTTP:Cookie !\smredir=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://m.example.org%REQUEST_URI [R,L]
【讨论】:
我知道这个话题有点老了......但是如果逻辑颠倒我需要改变什么?这是:example.com 上的移动网站和 s.example.com 上的普通网站? 那么设置 cookie[smredir] 为 1 会阻止重定向? @ToniMichelCaubet 将其设置为 0 应该可以防止后续请求的重定向。在带有?noredirect=true
的第一个请求中,它通过跳过下一条规则(带有S
标志)来避免重定向
在安卓(galaxy S6)上试过这个,profile
值都不存在,这不起作用【参考方案3】:
我进一步修改了 Tim Stone 的解决方案。这允许 cookie 处于 2 种状态,1 表示移动,0 表示完整。当移动 cookie 设置为 0 时,即使是移动浏览器也会访问整个网站。
代码如下:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine On
# Check if mobile=1 is set and set cookie 'mobile' equal to 1
RewriteCond %QUERY_STRING (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%HTTP_HOST]
# Check if mobile=0 is set and set cookie 'mobile' equal to 0
RewriteCond %QUERY_STRING (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%HTTP_HOST]
# cookie can't be set and read in the same request so check
RewriteCond %QUERY_STRING (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]
# Check if this looks like a mobile device
RewriteCond %HTTP:x-wap-profile !^$ [OR]
RewriteCond %HTTP_USER_AGENT "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %HTTP:Profile !^$
# Check if we're not already on the mobile site
RewriteCond %HTTP_HOST !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %HTTP:Cookie !\mobile=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://m.example.com%REQUEST_URI [R,L]
</IfModule>
【讨论】:
比公认的答案更好的代码,它也更通用,更容易使用。谢谢! 知道如何避免将特定页面重定向到移动网站吗?请看***.com/questions/18871314/…谢谢! 这对我很有用。但是,如何将用户重定向回整个站点? @user583576 将 ?mobile=0 添加到任何 uri 应重定向回整个站点 在 RewriteCond HTTP:Cookie 行上,为什么在“mobile”中的“m”之前有反斜杠?【参考方案4】:感谢 Tim Stone、naunu 和 Kevin Bond,这些答案对我很有帮助。这是我对您的代码的改编。我添加了从 m.example.com 重定向回桌面站点的功能,以防用户不使用移动设备访问该站点。另外我添加了一个环境变量来保存 http/https 请求:
# Set an environment variable for http/https.
RewriteCond %HTTPS =on
RewriteRule ^(.*)$ - [env=ps:https]
RewriteCond %HTTPS !=on
RewriteRule ^(.*)$ - [env=ps:http]
# Check if m=1 is set and set cookie 'm' equal to 1.
RewriteCond %QUERY_STRING (^|&)m=1(&|$)
RewriteRule ^ - [CO=m:1:example.com]
# Check if m=0 is set and set cookie 'm' equal to 0.
RewriteCond %QUERY_STRING (^|&)m=0(&|$)
RewriteRule ^ - [CO=m:0:example.com]
# Cookie can't be set and read in the same request so check.
RewriteCond %QUERY_STRING (^|&)m=0(&|$)
RewriteRule ^ - [S=1]
# Check if this looks like a mobile device.
RewriteCond %HTTP:x-wap-profile !^$ [OR]
RewriteCond %HTTP_USER_AGENT "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %HTTP:Profile !^$
# Check if we're not already on the mobile site.
RewriteCond %HTTP_HOST !^m\.
# Check if cookie is not set to force desktop site.
RewriteCond %HTTP_COOKIE !^.*m=0.*$ [NC]
# Now redirect to the mobile site preserving http or https.
RewriteRule ^ %ENV:ps://m.example.com%REQUEST_URI [R,L]
# Check if this looks like a desktop device.
RewriteCond %HTTP_USER_AGENT "!(android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile)" [NC]
# Check if we're on the mobile site.
RewriteCond %HTTP_HOST ^m\.
# Check if cookie is not set to force mobile site.
RewriteCond %HTTP_COOKIE !^.*m=1.*$ [NC]
# Now redirect to the mobile site preserving http or https.
RewriteRule ^ %ENV:ps://example.com%REQUEST_URI [R,L]
这似乎工作正常,除了一件事:当我使用桌面设备访问桌面站点并访问 m.example.com/?m=1 时,我被重定向到 example.com。当我再次尝试时,我“停留”在 m.example.com。似乎 cookie 第一次没有设置和/或读取正确。
也许有更好的方法来确定设备是否是桌面设备,我只是从上面否定了设备检测。
我想知道这样是否可以检测到所有移动设备。在 Tim Stone 和 naunu 的代码中,这部分要大得多。
【讨论】:
【参考方案5】:同样,如果您想重定向到子文件夹而不是子域,请执行以下操作:
利用 Kevin 的出色解决方案,您可以将其添加到站点根目录中的 .htaccess 文件中:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine On
# Check if mobile=1 is set and set cookie 'mobile' equal to 1
RewriteCond %QUERY_STRING (^|&)mobile=1(&|$)
RewriteRule ^ - [CO=mobile:1:%HTTP_HOST]
# Check if mobile=0 is set and set cookie 'mobile' equal to 0
RewriteCond %QUERY_STRING (^|&)mobile=0(&|$)
RewriteRule ^ - [CO=mobile:0:%HTTP_HOST]
# cookie can't be set and read in the same request so check
RewriteCond %QUERY_STRING (^|&)mobile=0(&|$)
RewriteRule ^ - [S=1]
# Check if this looks like a mobile device
RewriteCond %HTTP:x-wap-profile !^$ [OR]
RewriteCond %HTTP_USER_AGENT "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %HTTP:Profile !^$
# Check if we're not already on the mobile site
RewriteCond %HTTP_HOST !^m\.
# Check to make sure we haven't set the cookie before
RewriteCond %HTTP:Cookie !\mobile=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://www.mysite.com/m/ [R]
</IfModule>
然后,在 /m/
文件夹中,添加或创建一个 .htaccess,其中包含以下内容:
#Begin user agent loop fix
RewriteEngine Off
RewriteBase /
#End user agent loop fix
我知道这不是对这个问题的直接回答,但有人(比如我)可能会偶然发现这个问题,并想知道如何实现该方法。
【讨论】:
【参考方案6】:对于像domain.com/m/
这样的手机
RewriteCond %HTTP_REFERER !^http://(.*).domain.com/.*$ [NC]
RewriteCond %REQUEST_URI !^/m/.*$
RewriteCond %HTTP_USER_AGENT "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /m/ [L,R=302]
【讨论】:
【参考方案7】:<IfModule mod_rewrite.c>
RewriteEngine On
# https forcefully redirection
RewriteCond %HTTPS !=on
RewriteRule ^(.*)$ https://%HTTP_HOST%REQUEST_URI [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
# redirect on mobile site or other domain
RewriteCond %QUERY_STRING !^desktop
RewriteCond %HTTP_USER_AGENT "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos" [NC]
RewriteRule ^$ https://m.example.com/ [L,R=302]
</IfModule>
【讨论】:
没有任何解释的代码很少有帮助。 Stack Overflow 是关于学习的,而不是提供 sn-ps 来盲目复制和粘贴。请编辑您的问题并解释它如何回答所提出的具体问题。见How to Answer。【参考方案8】:首先,到以下网址下载mobile_detect.php文件:
http://code.google.com/p/php-mobile-detect/
在您的索引或主页上插入以下代码:
<?php
@include("Mobile_Detect.php");
$detect = new Mobile_Detect();
if ($detect->isMobile() && isset($_COOKIE['mobile']))
$detect = "false";
elseif ($detect->isMobile())
header("Location:http://www.yourmobiledirectory.com");
?>
【讨论】:
【参考方案9】:你也可以试试这个。感谢已删除脚本的原作者
/mobile.class.php
<?php
/*
=====================================================
Mobile version detection
-----------------------------------------------------
compliments of http://www.buchfelder.biz/
=====================================================
*/
$mobile = "http://www.stepforth.mobi";
$text = $_SERVER['HTTP_USER_AGENT'];
$var[0] = 'Mozilla/4.';
$var[1] = 'Mozilla/3.0';
$var[2] = 'AvantGo';
$var[3] = 'ProxiNet';
$var[4] = 'Danger hiptop 1.0';
$var[5] = 'DoCoMo/';
$var[6] = 'Google Chtml Proxy/';
$var[7] = 'UP.Browser/';
$var[8] = 'SEMC-Browser/';
$var[9] = 'J-PHONE/';
$var[10] = 'PDXGW/';
$var[11] = 'ASTEL/';
$var[12] = 'Mozilla/1.22';
$var[13] = 'Handspring';
$var[14] = 'Windows CE';
$var[15] = 'PPC';
$var[16] = 'Mozilla/2.0';
$var[17] = 'Blazer/';
$var[18] = 'Palm';
$var[19] = 'WebPro/';
$var[20] = 'EPOC32-WTL/';
$var[21] = 'Tungsten';
$var[22] = 'Netfront/';
$var[23] = 'Mobile Content Viewer/';
$var[24] = 'PDA';
$var[25] = 'MMP/2.0';
$var[26] = 'Embedix/';
$var[27] = 'Qtopia/';
$var[28] = 'Xiino/';
$var[29] = 'BlackBerry';
$var[30] = 'Gecko/20031007';
$var[31] = 'MOT-';
$var[32] = 'UP.Link/';
$var[33] = 'Smartphone';
$var[34] = 'portalmmm/';
$var[35] = 'Nokia';
$var[36] = 'Symbian';
$var[37] = 'AppleWebKit/413';
$var[38] = 'UPG1 UP/';
$var[39] = 'RegKing';
$var[40] = 'STNC-WTL/';
$var[41] = 'J2ME';
$var[42] = 'Opera Mini/';
$var[43] = 'SEC-';
$var[44] = 'ReqwirelessWeb/';
$var[45] = 'AU-MIC/';
$var[46] = 'Sharp';
$var[47] = 'SIE-';
$var[48] = 'SonyEricsson';
$var[49] = 'Elaine/';
$var[50] = 'SAMSUNG-';
$var[51] = 'Panasonic';
$var[52] = 'Siemens';
$var[53] = 'Sony';
$var[54] = 'Verizon';
$var[55] = 'Cingular';
$var[56] = 'Sprint';
$var[57] = 'AT&T;';
$var[58] = 'Nextel';
$var[59] = 'Pocket PC';
$var[60] = 'T-Mobile';
$var[61] = 'Orange';
$var[62] = 'Casio';
$var[63] = 'HTC';
$var[64] = 'Motorola';
$var[65] = 'Samsung';
$var[66] = 'NEC';
$result = count($var);
for ($i=0;$i<$result;$i++)
$ausg = stristr($text, $var[$i]);
if(strlen($ausg)>0)
header("location: $mobile");
exit;
?>
只需编辑$mobile = "http://www.stepforth.mobi";
【讨论】:
它对数组元素单独编号,这很愚蠢。它在 66 个单独的命令中创建数组,而不是一个数组初始化,这很慢。它对数组使用 for 循环而不是 foreach 循环,这不是惯用的。 它正在使用 stristr 进行搜索。此命令返回匹配的文本!如果匹配的话,它只需要一个是/否,这很荒谬。 (它甚至没有使用stripos!这仍然很糟糕,但没有那么糟糕。)然后将结果保存在一个变量中并实际检查长度,这令人难以置信的愚蠢和缓慢。 (只需将结果用作布尔值,无需计算长度。)它不使用 preg_match 来编译搜索模式并一次完成所有操作,而不是在一个缓慢的循环中。 这里的每一行代码都很糟糕,而且表现出的知识和技能都很差。 感谢您的反馈,我会将其作为学习曲线。尽管我不是原作者【参考方案10】:或者你可以试试这个:
?php
/**
* Mobile Detect
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class Mobile_Detect
protected $accept;
protected $userAgent;
protected $isMobile = false;
protected $isAndroid = null;
protected $isAndroidtablet = null;
protected $isIphone = null;
protected $isIpad = null;
protected $isBlackberry = null;
protected $isBlackberrytablet = null;
protected $isOpera = null;
protected $isPalm = null;
protected $isWindows = null;
protected $isWindowsphone = null;
protected $isGeneric = null;
protected $devices = array(
"android" => "android.*mobile",
"androidtablet" => "android(?!.*mobile)",
"blackberry" => "blackberry",
"blackberrytablet" => "rim tablet os",
"iphone" => "(iphone|ipod)",
"ipad" => "(ipad)",
"palm" => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
"windows" => "windows ce; (iemobile|ppc|smartphone)",
"windowsphone" => "windows phone os",
"generic" => "(kindle|mobile|mmp|midp|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini)");
public function __construct()
$this->userAgent = $_SERVER['HTTP_USER_AGENT'];
$this->accept = $_SERVER['HTTP_ACCEPT'];
if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']))
$this->isMobile = true;
elseif (strpos($this->accept, 'text/vnd.wap.wml') > 0 || strpos($this->accept, 'application/vnd.wap.xhtml+xml') > 0)
$this->isMobile = true;
else
foreach ($this->devices as $device => $regexp)
if ($this->isDevice($device))
$this->isMobile = true;
/**
* Overloads isAndroid() | isAndroidtablet() | isIphone() | isIpad() | isBlackberry() | isBlackberrytablet() | isPalm() | isWindowsphone() | isWindows() | isGeneric() through isDevice()
*
* @param string $name
* @param array $arguments
* @return bool
*/
public function __call($name, $arguments)
$device = substr($name, 2);
if ($name == "is" . ucfirst($device) && array_key_exists(strtolower($device), $this->devices))
return $this->isDevice($device);
else
trigger_error("Method $name not defined", E_USER_WARNING);
/**
* Returns true if any type of mobile device detected, including special ones
* @return bool
*/
public function isMobile()
return $this->isMobile;
protected function isDevice($device)
$var = "is" . ucfirst($device);
$return = $this->$var === null ? (bool) preg_match("/" . $this->devices[strtolower($device)] . "/i", $this->userAgent) : $this->$var;
if ($device != 'generic' && $return == true)
$this->isGeneric = false;
return $return;
【讨论】:
这对于 OP 的要求来说有些过分了 认真的吗?在“Hello World”中展示了所有技能……非常适合面试。 :)以上是关于使用 htaccess 进行移动重定向的主要内容,如果未能解决你的问题,请参考以下文章