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

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

+4
source share
4 answers

Of course 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

+5
source

Little Googling even has a convenient generator. http://detectmobilebrowsers.mobi/

0
source

To add @Tommy to the answer, if you want to go through the URI, change the RewriteRules to the following:

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

Otherwise, you will redirect all requests to the home page of the mobile phone (although this may be what you want).

0
source

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

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

You will notice that the [NC] nocase flag is set , which causes the RewriteRule to match case-insensitively.

That is, it does not matter whether user agent strings are displayed as uppercase, camel, or lowercase in a consistent URI. (e.g. IPHONE vs iPhone vs iphone).

0
source

Source: https://habr.com/ru/post/1314973/


All Articles