Configuring Device and Geo-Targeting Smart Routing
Distributing different download links for mobile apps or language-specific landing pages fragments marketing campaigns across multiple URLs. This guide configures intelligent edge routing rules that inspect client User-Agent strings and Cloudflare Anycast geographic tokens to redirect each visitor to the appropriate endpoint in under 10ms.
Prerequisites
- Workspace permissions:
Owner,Admin, orMemberrole in your Shaf organization. - Platform-specific destination URLs (e.g., Apple App Store ID, Google Play package link, desktop landing page).
- ISO 3166-1 alpha-2 two-letter country codes for regional destinations (e.g.,
US,GB,DE,JP).
Implementation Recipe
Configure Smart Routing via API or Dashboard
Assign conditional routing destinations to your short link record:
curl -X POST "https://shaf.app/api/v1/links" \ -H "Authorization: Bearer $SHAF_API_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "url": "https://acme.com/download-desktop", "slug": "get-app", "domain": "shaf.is", "apple": "https://apps.apple.com/app/id123456789", "google": "https://play.google.com/store/apps/details?id=com.acme.app", "geo": { "GB": "https://acme.co.uk/app", "DE": "https://acme.de/app", "FR": "https://acme.fr/app" } }'- Open the Link Creation or Edit modal.
- Enter the default fallback in Destination URL (used for desktop or unmatched traffic).
- Under Device Smart Routing, enter your Apple App Store URL and Google Play Store URL.
- Under Geo-Targeting, click + Add Country Rule, select the country code, and provide the regional URL.
- Click Save Link.
Edge Rule Evaluation Hierarchy
When a visitor requests the short link, Shaf evaluates rules in strict sequence at the nearest edge PoP:
flowchart TD A[Visitor Request] --> B{Geo Match?<br/>ISO Country Code} B -->|Match Found| C[Redirect to Regional URL] B -->|No Match| D{Device Type?} D -->|iOS / iPadOS| E[Redirect to Apple App Store] D -->|Android| F[Redirect to Google Play] D -->|Desktop / Other| G[Redirect to Default Destination URL]- Geographic Check: Evaluates Cloudflare edge country metadata (
request.cf.country). If a matching ISO code exists, the regional URL is served immediately. - Device OS Check: If no geo rule matches, the edge evaluates client
User-Agentheaders for iOS (iPhone,iPad,iPod) or Android (Android). - Default Destination: Desktop browsers and unmapped clients redirect to the primary
url.
- Geographic Check: Evaluates Cloudflare edge country metadata (
Verification
Simulate different client environments using curl with custom User-Agent and header flags:
-
Simulate an iOS client:
curl -I -A "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X)" https://shaf.is/get-appExpected output:
HTTP/2 308 Permanent Redirect location: https://apps.apple.com/app/id123456789 x-shaf-route: device-apple -
Simulate an Android client:
curl -I -A "Mozilla/5.0 (Linux; Android 14; Pixel 8)" https://shaf.is/get-appExpected output:
HTTP/2 308 Permanent Redirect location: https://play.google.com/store/apps/details?id=com.acme.app x-shaf-route: device-google -
Simulate a desktop client:
curl -I -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" https://shaf.is/get-appExpected output:
HTTP/2 308 Permanent Redirect location: https://acme.com/download-desktop x-shaf-route: default
Common Gotchas
| Issue / Error | Root Cause | Solution |
|---|---|---|
| iPad directed to Desktop URL | Safari on iPadOS requests desktop websites by default (Macintosh; Intel Mac OS X). |
Shaf matches touch indicators where available; specify explicit iOS landing pages with fallback options. |
| Country code mismatch | Lowercase or 3-letter ISO code provided in geo object (e.g. usa or us). |
Use standard two-letter uppercase ISO 3166-1 alpha-2 format (e.g. US, GB, JP). |
| VPN redirects to incorrect nation | Visitor IP terminates at a VPN exit node in another country. | Edge routing evaluates the public egress IP presented to Cloudflare Anycast nodes. |
| Query parameters missing on app store | App Store / Play Store tracking tokens were not appended. | Append campaign referral tags directly into the store URLs (?pt=...&ct=...). |