Ideally, of course, all of the links on your website are allowed to be opened in the app, regardless of subdomains or www
redirects, and then you use the content of apple-app-site-association
to limit which ones actually will open in the app.
Let's assume you want everything on your domain (e.g. coursicle.com
) to be eligible to be opened as a deep link. Then you'll want to configure your entitlements in Xcode as applinks:*.coursicle.com
. No problem because you own coursicle.com, right? But it's very likely that you're redirecting coursicle.com
to www.coursicle.com
and Apple's going to check coursicle.com/.well-known/apple-app-site-association
and/or coursicle.com/apple-app-site-association
, which is going to redirect to the www
equivalent. Because of Apple's "no redirects" stipulation, your deep links will fail.
@33-B01's answer points this out and offers a workaround. We were considering this solution, by making it so that on iPhones any link on our site that we wanted deep linked would have a subdomain link
added to it. Moreover, our entitlements would contain applinks:link.coursicle.com
and link.coursicle.com/apple-app-site-association
would return our JSON without any redirects. This meant for desktop users we'd display www.coursicle.com/unc/?search=math
but for iPhone user agents we'd show link.coursicle.com/unc/?search=math
. If an iPhone user tapped on a link.coursicle.com/unc/?search=math
link and they had the app, it'd open directly in the app. If they didn't, we'd have a redirect rule that takes all link.coursicle.com
links and redirects them to www.coursicle.com
, allowing our application code to handle this user now that we know they don't have the app.
This solution actually killed two birds with one stone. If the user is on a coursicle.com
link in Safari, in order to get a deep link to actually open in the app we need to provide a different subdomain per Apple's requirements, otherwise it's assumed that the user wants to stay in Safari (I this this is pretty silly and I do not understand the reasoning). So because all of the desired deep links were link.coursicle.com
and because we could easily host our JSON at link.coursicle.com/apple-app-site-association
without any redirects, it works.
There's just one problem: you don't have control over how others link to your site. What happens if someone on desktop sends the link www.coursicle.com/unc/?search=math
to someone on an iPhone who has the app installed? Well applinks:coursicle.com
isn't in our entitlements so the link would open in Safari.
To get the ideal solution, despite having a www redirect, all you have to do is prevent the www redirection for the specific files you know Apple is going to request. In Apache, it can be done via the following:
# force www. because the local storage/cookies does discriminate
# unless it's the apple-app-site-association which won't tolerate a redirect so we
# have to have it load the appropriate file directly
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !^/apple-app-site-association$ [NC]
RewriteCond %{REQUEST_URI} !^/.well-known/apple-app-site-association$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 [R=301,L]
This might cause some of your subsequent rules to behave a little funny because you expect a www redirect, so you can always add the following to make it so that if the URL does exactly match apple-app-site-association
, you have Apache serve it directly from the file system:
# Manually specify where to find the apple-app-site-association, may not be necessary depending on your configuration
Alias "/apple-app-site-association" "/var/www/coursicle.com/shared/link/nativeDeepLink/apple-app-site-association.php"
Alias "/.well-known/apple-app-site-association" "/var/www/coursicle.com/shared/link/nativeDeepLink/apple-app-site-association.php"