Skip to content
AppQR

How to redirect a URL by device: do it yourself or use a hosted link

Short answer

Point the link at a server that reads the visitor's User-Agent header, picks a destination for Android, iPhone, Windows, Mac or Linux, and answers with a 302 redirect marked Cache-Control no-store. A Cloudflare Worker or an nginx map does it in a few lines. A hosted smart link does the same without code, and adds a QR code and scan counts.

Last updated

A device redirect is one address that sends each visitor somewhere different: Android to Google Play, iPhone to the App Store, computers to their downloads, and everyone else to your website. The decision has to happen on a server, because a QR code or a link in a README cannot run any code of its own.

How does a device redirect work?

Every request a browser makes carries a User-Agent header naming its system, such as Android, iPhone, Windows NT, Macintosh or X11; Linux. Chromium-based browsers also send Sec-CH-UA-Platform, with a value such as "Android", "Windows" or "macOS". Your server reads these, picks a destination, and answers with a redirect:

HTTP/1.1 302 Found
Location: https://play.google.com/store/apps/details?id=com.example.app
Cache-Control: no-store

The order of the checks matters. Android user agents also contain Linux, and iPhone user agents contain like Mac OS X, so test for phones first.

A Cloudflare Worker

This Worker is the whole redirect. Deploy it on a route such as get.example.com/*.

const DESTINATIONS = {
  android: 'https://play.google.com/store/apps/details?id=com.example.app',
  ios: 'https://apps.apple.com/app/id123456789',
  windows: 'https://example.com/download/windows',
  macos: 'https://example.com/download/mac',
  linux: 'https://flathub.org/apps/com.example.App',
};
const FALLBACK = 'https://example.com/';

function platformOf(request) {
  const ua = request.headers.get('User-Agent') || '';
  const hint = (request.headers.get('Sec-CH-UA-Platform') || '').replaceAll('"', '');
  // Phones first: Android says "Linux" and iOS says "like Mac OS X".
  if (hint === 'Android' || /Android/i.test(ua)) return 'android';
  if (hint === 'iOS' || /iPhone|iPad|iPod/.test(ua)) return 'ios';
  if (hint === 'Windows' || /Windows NT/.test(ua)) return 'windows';
  if (hint === 'macOS' || /Macintosh/.test(ua)) return 'macos';
  if (/CrOS/.test(ua)) return null; // Chromebooks get the fallback
  if (hint === 'Linux' || /Linux|X11/.test(ua)) return 'linux';
  return null;
}

export default {
  async fetch(request) {
    const target = DESTINATIONS[platformOf(request)] ?? FALLBACK;
    return new Response(null, {
      status: 302,
      headers: {
        Location: target,
        'Cache-Control': 'no-store',
        Vary: 'User-Agent, Sec-CH-UA-Platform',
      },
    });
  },
};

The same function works in Node, Deno or any server that gives you the request headers.

An nginx map

If you already run nginx, a map can do it without any code. Put the map in the http block. Regular expressions in a map are tried in the order they are written, and the first match wins.

map $http_user_agent $device_target {
    default              "https://example.com/";
    "~*android"          "https://play.google.com/store/apps/details?id=com.example.app";
    "~(iPhone|iPad|iPod)" "https://apps.apple.com/app/id123456789";
    "~Windows NT"        "https://example.com/download/windows";
    "~Macintosh"         "https://example.com/download/mac";
    "~CrOS"              "https://example.com/";
    "~(Linux|X11)"       "https://flathub.org/apps/com.example.App";
}

Then, inside your existing server block:

location = /app {
    add_header Cache-Control "no-store" always;
    add_header Vary "User-Agent" always;
    return 302 $device_target;
}

This version reads only the user agent, which is enough for most visitors.

Why does a device redirect send people to the wrong place?

  • iPads that look like Macs. Safari on iPad usually asks for the desktop version of sites and sends a user agent identical to a Mac’s. A server cannot tell them apart, so those iPads get your Mac destination. Why an iPad is detected as a Mac covers the fixes.
  • Phones in desktop mode. A phone set to request desktop sites sends a computer’s user agent and gets that computer’s destination.
  • In-app browsers. Links opened inside social and chat apps load in the app’s own browser. These usually keep the system in the user agent, so detection still works, but some handle store links differently from the phone’s main browser. Test in the apps your link will be shared in.
  • Link-preview fetchers and crawlers. Chat apps, social networks and search engines fetch your link to build a preview or index it. Their user agents rarely name a device, so they get your fallback. Make the fallback a page that makes sense to anyone, and leave these out of your counts if you record visits.
  • Caching. If a CDN or proxy caches the first redirect, every later visitor gets the same destination. Send Cache-Control: no-store. Vary: User-Agent tells caches that the answer depends on the device, but many CDNs do not key their cache on it, so do not rely on Vary alone. Avoid 301, which browsers may remember for good.
  • Frozen version numbers. Browsers no longer report exact versions: Mac browsers report the same Intel Mac version on Apple Silicon, and Windows 11 reports Windows NT 10.0. Route by system, not by version.
  • Things that are none of the five. Smart TVs and game consoles often say Linux or Windows NT in their user agents. Check for them before the desktop rules if they matter to you.
Detail Do it yourself Hosted smart link
Cost Usually free or nearly free on a Worker or your own server Free plans exist; paid plans add features
Changing a destination Edit and deploy code or config Edit a form
Your own domain Yes Depends on the service and plan
QR code Make one separately Usually included
Counting visits Build it yourself Usually included
Keeping detection up to date Your job The service’s job
Depends on a third party No Yes: check what happens if you stop paying or the service closes

If you have one link, a domain and someone who is happy to maintain a Worker, doing it yourself is reasonable and keeps you in control. A hosted link makes more sense when several people need to change destinations, when you want scan counts, or when the link goes on print that cannot be changed later.

How AppQR does it

AppQR’s redirect works like the Worker above. It reads the User-Agent and, from Chromium browsers, Sec-CH-UA-Platform, and answers with a 302 marked Cache-Control: no-store. Each link has a destination for Android, iPhone and iPad, Windows, Mac and Linux, and a default.

  • Anything it cannot identify with confidence, including Chromebooks, game consoles and smart TVs other than Android TV, goes to the default.
  • Link previews and crawlers are sent on to a destination but are not counted as scans.
  • There is no page or advert before the redirect, and it runs no code on the visitor’s device. That means an iPad asking for desktop sites gets the Mac destination.
  • Test this link in the dashboard shows which address each kind of visitor will get.

See How platform routing works for the details and Pricing for what each plan includes.

Questions people also ask

Should a device redirect use 301 or 302?

Use 302 or 307. Browsers can remember a 301 for good, so a visitor who later opens the link on another device, or after you change a destination, may skip your server and go to the old address.

Why does everyone get the same destination after I put a CDN in front?

The CDN cached the first answer and gave it to everyone. Mark the redirect Cache-Control no-store so it is never cached, or make sure your CDN caches it separately per device, which most do not do by default.

Can I detect the device from the IP address?

No. An IP address can tell you roughly where a visitor is, not which device they use. The User-Agent header, and the Sec-CH-UA-Platform header in Chromium browsers, are what you have.

Why do link previews in chat apps go to my default page?

Chat and social apps fetch the link themselves to build the preview, and their fetchers do not claim to be a phone or a computer. Make the default a page that makes sense to anyone, such as your website.

Is it safe to redirect by User-Agent for search engines?

Sending crawlers to the same place as an unidentified visitor, your default, is normal for a redirect link. Do not give crawlers a different page from the one people see on the same device.

Does AppQR open my app if it is installed?

No. An AppQR link always opens a web address, such as your App Store or Google Play page. Links that open an installed app directly are not available.

Try it with your own links

One short link and QR code that sends each device to its own page. Free for 2 links, with no card and no time limit.