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

笑着哭i 提交于 2019-12-03 00:43:56

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

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

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).

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!