I'm building a hybrid Facebook app: desktop users will be directed to a Page Tab, while mobile users will be directed to a Mobile Web App.
I need to figure out a fast, accurate way to detect the user's device and perform the appropriate redirect – ideally something not too complex. Can it be done?
…Yes! It can absolutely be done.
Facebook automatically redirects mobile users attempting to access a Canvas App (
apps.facebook.com/your_app
) to its Mobile Web App equivalent.So, just set up a server-side redirect to
apps.facebook.com/your_app
, and set up a client-side redirect from the Canvas App to the Page Tab.Facebook will redirect mobile users to your Mobile Web App; desktop users will land on your Canvas App, which will immediately redirect them to your Page Tab.
The server-side redirect looks like this, in PHP:
The client-side redirect looks like this, in JavaScript:
This is a reliable solution, much better than trying to match user agents. Your list of user agents might be different from Facebook's; and indeed, Facebook is very inconsistent with how it performs mobile redirects.