Force a page to load inside Facebook Canvas

As a Facebook apps developers, quite a few times you may have encountered a situation where you had to make sure that a page loads inside Facebook’s App Canvas.

There are couple of ways of doing this, but I found this one most easy to use:

How it works:

1. This code on page load will check whether the referrer was Facebook or not. This is done because, when a app loads in Facebook canvas, Facebook makes a POST request to the default page.

2. If the request was from Facebook, nothing happens. If it was not, then it redirects the browser to the app url.

Thats it. Its that simple.