Promote your app on social more effectively with this PHP redirect

A simpler way to get your app users to the right app store when promoting it on social media is to use this PHP redirect script.

Image: Screenshot by Mary Weilage/TechRepublic

Services that shorten URLs such as Bitly and TinyURL are particularly helpful to app developers, because a link to your app in the marketplace tends to look something like this:

When I first released my latest game, Melodious, on Google Play, I found shortened URLs worked great not only for sharing in game with other players, but also on Facebook and Twitter. However, as the popularity of Melodious grew, and I made it available for iOS and Kindle Fire, even with shortened URLs, the user experience was less than ideal. My initial share sheet text was simply:

Have you tried Melodious? Download it today!

Once the game was available in multiple markets, I found myself having to tweet things like:

Have you tried Melodious? Download it today!

On Google Play:

On Apple iTunes:

On Amazon Kindle:

Not only is the above example a lot to expect the user to read, but it also violates some of the length restrictions of some social media outlets. Plus, it's horrible for sharing via SMS, sometimes breaking up into multiple messages that split on a URL, making the link completely worthless.

Fortunately, I know a bit of PHP, and when backed into a corner I'm not afraid to use it! My solution was to create a simple redirect script that would send a user to a particular store based on the web client accessing my page. So if the user hit the link from a Safari client, I'd send them to iTunes, a Silk client would take you to Amazon's App Store, Chrome would go to Google Play, and as a fallback anything else would display my lengthy but complete list of all platforms and links where a user could get the app.

This approach has worked really well for me. So while I usually share Java code in my TechRepublic columns, in this instance, I thought I'd share my PHP script. Enjoy!

$SAFARI_URL = " ";
$OTHER_URL = "";
$AMAZON_URL =  “";
function str_present($str,$substr)
	$pos = strpos($str,$substr);
	if($pos === false) {
 		return false;
	} else {
 		return true;
if (str_present($HTTP_USER_AGENT, "Silk")) {
   Header ("Location: " . $AMAZON_URL);
} else if (str_present($HTTP_USER_AGENT, "Chrome")) {
    Header ("Location: " . $CHROME_URL);
} else if (str_present($HTTP_USER_AGENT, "Safari")) {
    Header ("Location: " . $SAFARI_URL);
} else {
    Header ("Location: " . $OTHER_URL);

Figure A

Image: Screenshot by William J. Francis/TechRepublic
My URL when accessed from an Android phone or tablet.

Figure B

Image: Screenshot by William J. Francis/TechRepublic
My URL when accessed from an iPhone or iPad.

By William J. Francis

William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige...