简体   繁体   中英

nginx: redirect mobile requests to /mobile/$uri

I am trying to setup my nginx to redirect all the requests from mobile devices to /mobile/$uri i came up with this solution but it doesn't seem to work. Is it a syntax problem or a misunderstanding of the whole redirecting concept.

if ($http_user_agent ~* '(iPhone|iPod|android|blackberry)') {
     rewrite     ^(.*)   http://xxxx.org/mobile/$1 permanent;
}

When i use my android phone i am getting something like xxx.org/mobile/mobile/mobile/mobile....

Any ideas?Any suggestions?

You should use different locations:

location / {
    if ($http_user_agent ~* '(iPhone|iPod|android|blackberry)') {
        return 301 /mobile$request_uri;
    }
}

location /mobile/ {

}

btw, http://nginx.org/r/return

    #initliaze mobile
    set $mobile "";

    if ($request_uri !~* "^/mobile.*" ){
        set $mobile Y;
    }
    if ($http_user_agent ~* (iPhone|iPod|android|blackberry) ) {
        set $mobile "${mobile}E";
    }

    if ( $http_referer !~* "xxx\.org" ){
       set $mobile "${mobile}S";
    }

    if ( $host ~* "xxx\.org" ){
       set $mobile "${mobile}S";
    }

    if ($mobile = YESS){
        rewrite ^ $scheme://$host/mobile$request_uri ;
    }

使用http://detectmobilebrowsers.com/它包含所有Web服务器和客户端/服务器端语言的脚本。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM