Detect iPhone browser in .htaccess/apache and redirect to iPhone site

后端 未结 4 1392
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 15:29

Is it possible to detect iPhone browser agent in .htaccess and redirect the user to the correct iPhone page?

相关标签:
4条回答
  • 2021-02-03 15:58

    Here's a simplified RewiteRule that combines the mobile user agents into a single condition:

    # Redirect Mobile Devices
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile" [NC]
    RewriteRule ^(.*)$ http://m.example.com/$1 [R=301,L]
    

    You'll notice that the [NC] nocase flag is set, which causes the RewriteRule to be matched in a case-insensitive manner.

    That is, it doesn't care whether the user agents strings appear as upper-case, camel-case or lower-case in the matched URI. (e.g., IPHONE vs. iPhone vs. iphone).

    0 讨论(0)
  • 2021-02-03 16:06

    To add to @Tommy's response, if you want to pass through the URI, change the RewriteRules to the following:

    RewriteRule ^(.*)$ http://mobile.yourdomain.com$1 [R=301]
    

    Otherwise you'll be redirecting all requests to the mobile home page (though that might be what you want).

    0 讨论(0)
  • 2021-02-03 16:09

    A little Googling, even has a handy generator. http://detectmobilebrowsers.mobi/

    0 讨论(0)
  • 2021-02-03 16:16

    Sure you can -

       #redirect mobile browsers
        RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
        RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
        RewriteCond %{HTTP_USER_AGENT} ^.*BlackBerry.*$
        RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
        RewriteCond %{HTTP_USER_AGENT} ^.*Palm.*$
        RewriteRule ^(.*)$ http://mobile.yourdomain.com [R=301]
    

    Taken from here

    0 讨论(0)
提交回复
热议问题