使用og元标记(Open Graph)重定向PHP URL

4

我有一个在PHP中实现的服务器端重定向页面redirect.php,该页面使用PHP的header()函数来重定向用户:

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

有没有一种方法可以添加一些OG元标签(开放图谱),以便当有人在Facebook和类似网站上分享redirect.php页面时,这些属性会被应用?
<meta property="og:title" content="Facebook should load this when shared redirect.php"/>
1个回答

7

由于 redirect.php 在每次访问时都会重定向,因此这是不可能的。但是,我们可以允许 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');
}

参考资料


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接