手机浏览器与检测的.htaccess失败,Opera Mini的 [英] Mobile browser detection with .htaccess fails with Opera Mini

查看:264
本文介绍了手机浏览器与检测的.htaccess失败,Opera Mini的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法搞清楚这一个。我想直接移动流量到一个网站通过.htaccess的用户代理的移动版本嗅,像这样:

I'm having trouble figuring this one out. I'm trying to direct mobile traffic to the mobile version of a website via HTACCESS User-Agent sniffing, like so:

RewriteCond %{HTTP_USER_AGENT} (android|blackberry|ipad|iphone|ipod|iemobile|mini|mobi|palmos|webos|googlebot\-mobile) [NC]
RewriteCond %{HTTP_HOST} ^mobile\.mywebsite\.com$
RewriteRule ^([aA0-zZ9\-\/]*)/([^/]*)$ /index.php?page=$1&q=$2&q2=$3&setview=mobile [L,QSA]

字符串迷你|摩比是必须检测的Opera Mini浏览器。但是,这种情况不会发生。我也改变了字符串歌剧米和其他变化。仍然没有运气。

The string mini|mobi is where the Opera Mini browser should be detected. However, this does NOT happen. I've also changed that string to opera m and other variations. Still no luck.

这重写条件似乎与其他移动浏览器的工作,但不是歌剧,我不明白为什么。即使第二个条件,这将检查一个明确的指针移动子域名也无法传递的手机内容。

This rewrite condition appears to work with other mobile browsers, but not Opera, and I don't understand why. Even the second condition, which checks for an explicit pointer to the mobile. subdomain also fails to deliver the mobile content.

我不是一个.htaccess的大师,所以我的第一个猜测是,我的语法是错误的。但是,为什么这会起作用的其他移动浏览器,而不是Opera Mini的?

I'm not an HTACCESS guru, so my first guess is that my syntax is wrong. But why does this work on other mobile browsers and not Opera Mini?

为寻找谢谢!

我居然使出了不同的方法,通过使用PHP来检查头。出于某种原因,我的.htaccess文件中没有检测到某些头信息。尽管karlcow的回答会验证,我遇到了麻烦iPhone和Android第三方浏览器来验证。直到一个网页被传递到PHP跨preTER,我是能够读取信息。我想我只是不明白如何工作的。

I actually resorted to a different method by using PHP to check headers. For some reason my HTACCESS file did not detect certain header information. Even though karlcow's answer would validate, I was having trouble getting the iPhone and third-party Android browsers to validate. It wasn't until a page was passed to the PHP interpreter that I was able to read the information. I guess I just don't understand how that works.

不管怎样,我借了code从网上搜索,我放在一个库函数:

Anyway, I borrowed code from an Internet search that I placed into a library function:

function get_device_view() {    
    $view='desktop';
    if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
        $view='mobile';
    if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) || ((isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']))))
        $view='mobile';
    $mobile_ua=strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
    $mobile_agents=array(
        'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
        'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
        'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
        'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
        'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
        'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
        'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
        'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
        'wapr','webc','winw','winw','xda ','xda-');
    if(in_array($mobile_ua,$mobile_agents))
        $view='mobile';
    if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0)
        $view='mobile';
    if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows')>0)
        $view='desktop';
    return $view;
}

这个函数被调用为strapper过程的网站的一部分,这样的事情之前输出时, $视图返回,这样加载相应的页面和样式。

This function is called as part of a strapper process for the website so that before anything is output, the $view is returned so that appropriate pages and stylesheets are loaded.

此外,我修改了我的.htaccess文件包含以下内容:

Additionally, I modified my HTACCESS file to contain the following:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_COOKIE} !((^|;\s*)DD_VIEW=([^;]+))
RewriteCond %{HTTP_HOST} mobile\.buffalosbigdeal\.com
RewriteRule ^([aA0-zZ9\-\/]*)/*([^/]*)$ /index.php?page=$1&q=$2&q2=$3&setview=mobile [L,QSA]

正如你所看到的,我只重定向为移动显式调用。子域,而那些不包含它保留了 $视图在strapper过程设置cookie。

As you can see I'm only redirecting explicit calls for the mobile. subdomain, and those not containing a cookie which retains the $view set during the strapper process.

我的本意是用.htaccess中进行浏览器检测只;因为简单而下的mod_rewrite的力量premise。但是,在我的无奈,并在最后期限前,我通过妥协解决一个更笨重的方法,可能需要在未来更多的维护。

My original intent was to perform browser detection exclusively using HTACCESS; because of simplicity and under premise of mod_rewrite's power. But in my frustration, and to meet the deadline, I compromised by settling for a more chunky method that may require more maintenance in the future.

经验教训:构建基于浏览器的功能响应的网站(如:分辨率,触摸,和DOM Level),也不要对用户代理字符串倚重。难道我们还没有这样一个解决方案?

Lesson learned: Build responsive websites based on browser capabilities (ie: resolution, touch, and DOM level), and do not rely heavily on user-agent strings. Have we yet a solution like this?

感谢您寻找。

推荐答案

请注意,Opera Mini的字符串的模式是

Note that the pattern of Opera Mini string is

Opera/9.80 (J2ME/MIDP; Opera Mini/$CLIENT_VERSION/$SERVER_VERSION; U; $LANGUAGE) Presto/$PRESTO_VERSION

Mini是一个大写M。嗯,不过你把NC这是NOCASE,所以这不是它。 只是要确保你能尝试

Mini is with an uppercase M. Hmm but you put NC which is for nocase, so that's not it. Just to be sure could you try

RewriteCond %{HTTP_USER_AGENT} (android|blackberry|ipad|iphone|ipod|iemobile|mobi|palmos|webos|googlebot\-mobile) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Opera\ Mini

这篇关于手机浏览器与检测的.htaccess失败,Opera Mini的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆