Conditional URL Redirection

What is the name of the domain?

‘maginet.co.il’

What is the issue you’re encountering

I dont know how to make a conditional url redirection- with a user agent

What steps have you taken to resolve the issue?

So, I’m trying to make a conditional URL Redirection.
I want to make a QR Code to promote an application, and the “short link” will be my domain- and there will be a conditional redirect whether you’re arriving from iOS or Android- for iOS it will hit iTunes, and for Android will hit Play Store.
How can I do that?

Thanks in advance

If you want to use Cloudflare, then using a Worker you would determine based by the User-Agent, then redirect where you need to go.

Otherwise, some page and usage of JavaScript function or a complete library to determine by the mobile User-Agents.

However, nowadays there are Desktop browsers and extensions, other tools, which you can mimic other devices and change User-Agents, therefrom you would never know.

Nevertheless, for the majority and your case need, it’ll work.

Example Worker code which you can bound to a route domain.com/some-page/*:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const userAgent = request.headers.get('User-Agent').toLowerCase()
  
  // Define the URLs for Android and iOS
  const androidUrl = 'https://play.google.com/store/apps/details?id=com.example.app'  // Replace with your Android app link
  const iosUrl = 'https://apps.apple.com/us/app/your-app/id1234567890'  // Replace with your iOS app link
  
  // Check user-agent and determine the redirect URL
  if (userAgent.includes('android')) {
    return Response.redirect(androidUrl, 302)
  } else if (userAgent.includes('iphone') || userAgent.includes('ipad')) {
    return Response.redirect(iosUrl, 302)
  }

  // Default redirect or return response if not matching
  return Response.redirect('https://yourdomain.com/page-with-buttons/', 302)  // Replace with your default URL if needed
}

Otherwise, if not Android, iPad or iPhone, just redirect to a single page, add two buttons Google Play and Apple Store such as “Available on the AppStore” & “Android App on Google Play” or use predefined already.

slika

That way you leave a way out for the visitor to click on the one depending which device he/she’s using and install your application on their device.

This Page can be as well displayed on a route domain.com/installapp using another Worker to return simple HTML (no need for web hosting) following example from below:

Another way would be to upload your custom HTML file and use Cloudflare Pages project for this, therefrom use custom domain such as app.mydomain.com or download.mydomain.com to which Worker would redirect if no match Android nor iPad nor iPhone as user-agent.

If you’re using at least a Pro plan, using Snippets would help in your case, no need for a Workers:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.