2014-04-01 133 views
3

我在PHP redirect.php服務器端重定向頁面此頁面重定向用PHP header()功能的用戶:PHP重定向URL

header('location: mypage.html?test'); 

有沒有辦法添加一些OG meta標籤( Open Graph),當某人在Facebook和類似網站上共享redirect.php頁面時,這些屬性將被應用?

<meta property="og:title" content="Facebook should load this when shared redirect.php"/> 

回答

3

由於redirect.php重定向每當它的訪問或/ &執行這將是不可能的,但有一個簡單的if發言中,我們可以讓Facebook的調試器讀取頁面OG meta標籤如下

PHP

<?php 
if (in_array($_SERVER['HTTP_USER_AGENT'], array(
    'facebookexternalhit/1.1 (+https://www.facebook.com/externalhit_uatext.php)', 
    'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)' 
))) { 
    header("HTTP/1.1 200 OK"); 
    print '<html prefix="og: http://ogp.me/ns#"> 
       <head> 
        <title>{title}</title> 
        <meta property="og:title" content="{OG Title}" /> 
        <meta property="og:type" content="website" /> 
        <meta property="og:url" content="http://example.com" /> 
        <meta property="og:image" content="http://example.com/og_img.jpg" /> 
       </head> 
     </html>'; 
} 
else { 
    // You're not Facebook agent '__' 
    header('Location: mypage.html?test'); 
} 

請記住,這僅適用於Facebook bot,它會在Google或Twitter上爲此頁建立索引時出現問題,所以我建議您改用JavaScript location.href

參考

+0

我不知道這是否符合Facebook的服務條款。 – Aley