2014-04-01 134 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