In PHP you can use something like:
<?php
$iPod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
$webOS = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");
//do something with this information
if( $iPod || $iPhone ){
//browser reported as an iPhone/iPod touch -- do something here
$string = "Location: <<your itunes app link>>";
header($string);
die();
}else if($iPad){
//browser reported as an iPad -- do something here
$string = "Location: <<your itunes app link>>";
header($string);
die();
}else if($Android){
//browser reported as an Android device -- do something here
$string = "Location: <<Google Play Link>>";
header($string);
die();
}else if($webOS){
//browser reported as a webOS device -- do something here
$string = "Location: <<Your Page link>>";
header($string);
die();
}else{
//browser reported as PC -- do something here
$string = "Location: <<Your Page link>>";
header($string);
die();
}
?>
You can use links for iTunes or Android, respectively:
itms-apps://itunes.apple.com/app/<<App ID>>
market://details?id=<<Package id>>
I don´t remember the source, but at least it works for me for sharing in other apps like Whatsapp, but unfortunately is not working on Facebook.
The problem in Facebook is that they use the metadata of the final link on the path of redirections, and the link points to GooglePlay store.