user3304007
user3304007

Reputation: 470

.htacess - Redirect only if its android

I need to auto open page in chrome. This code opens in mobile chrome without any problems.

RedirectMatch 301 id/(.*)$ intent://remoteurl.com/cancel.php?id=$1#Intent;scheme=https;package=com.android.chrome;end

But need this work only if user is on Android. So I have tried this but it doesnt redirect.

RewriteCond %{HTTP_USER_AGENT} (android) [NC]
RedirectMatch 301 ^id/(.*)$ intent://remoteurl.com/cancel.php?id=$1#Intent;scheme=https;package=com.android.chrome;end

The problem is, this url scheme won't work if RewriteRule is used,

RewriteRule ^i/(.*)$ intent://remoteurl.com/cancel.php?id=$1#Intent;scheme=https;package=com.android.chrome;end [R=301,L]

but works with redirectMatch 301. However, redirectMatch 301 doesn't work if RewriteCond is used. So what is the correct way to do that ?

(Similar questions have already been asked, however this is for different url scheme intent://)

Upvotes: 0

Views: 106

Answers (1)

John Doe
John Doe

Reputation: 1424

You can use the one of the following code for redirecting the user if he is visiting from mobile ....

Using PHP:

<?php
function isMobile()
{
    return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
// If the user is on a mobile device, redirect them
if(isMobile())
{
    // Redirect to Some where ....
    header("Location: http://m.yoursite.com/");
}
?>

Using .htaccess:

  • Code 1
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} (iphone|ipad|android|blackberry|windows\ phone) [NC]
RewriteCond %{HTTP_HOST} ^\.m.yoursite.com\.com$ [NC]
RewriteRule ^ http://yoursite.com
  • Code 2
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !(iphone|ipad|android|blackberry|windows\ phone) [NC]
RewriteCond %{HTTP_HOST} ^m\.yoursite\.com$ [NC]
RewriteRule ^ http://www.yoursite.com

Upvotes: 1

Related Questions