如何将所有http重定向到https facebook页面?

时间:2012-01-18 11:44:15

标签: facebook url https

我想从此网址强制应用程序的所有父窗口: http://apps.facebook.com/app_name/ 对此: https://apps.facebook.com/app_name/

(http到https)

我该怎么做? 谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用类似的内容(请注意,这会使您的应用程序没有signed_request):

if (document.location.protocol=='http:'){
  document.location.protocol = 'https:';
}

如果您确实希望父框架(Facebook本身)切换到HTTPS,那么您需要知道应用程序中页面的URL

if (document.location.protocol=='http:'){
  window.top.location = 'https://YOUR_APPLICATION_PAGE_URL'
}

你可能知道你的应用程序的URL模式,并且知道它的Canvas URL,所以这样的东西可能适合:

if (document.location.protocol=='http:'){
  var applicationUrl = 'https://facebook.com/example-application';
  var canvasURL = 'http://example.com/facebook-canvas';
  var currentAppPageUrl = (document.location+'').replace(canvasURL, applicationUrl);
  window.top.location = currentAppPageUrl;
}

答案 1 :(得分:0)

Canvas应用程序位于您的域中,顶部位于Facebook域,因此在Facebook Canvas应用程序中调用window.top.location会导致从iframe访问跨域URL。

您只需要通过try catch block捕获异常。

if (document.location.protocol=='http:'){
    var applicationUrl = 'https://facebook.com/example-application';
    var canvasURL = location.protocol+'//'+location.host+location.pathname;
    var currentAppPageUrl = (document.location+'').replace(canvasURL, applicationUrl);
    try {
        window.top.location = currentAppPageUrl;
    } catch (e) {
    }
}