php重定向网址与og元标记(开放图) [英] php redirect url with og metatag (open graph)

查看:133
本文介绍了php重定向网址与og元标记(开放图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中有服务器端重定向页面 redirect.php 此页面使用PHP重定向用户 header()功能:

I have server side redirect page in PHP redirect.php this page redirects the user with PHP header() function:

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

有没有办法添加一些OG元标记(Open Graph)

Is there a way to add some OG meta tags (Open Graph) in a way that when someone shares the redirect.php page on Facebook and similar websites, these properties would be applied?

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


推荐答案

由于 redirect.php 每次访问时重定向或/&执行它是不可能的,但是使用简单的如果语句,我们可以允许Facebook调试器读取页面OG元标签如下

Since redirect.php redirects whenever it's accessed or/& executed it would be impossible, but with a simple if statement we can allow Facebook Debugger to read the page OG meta tags as below

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

Keep in mind this works only with Facebook bot which causes problems with indexing this page on Google or Twitter, so I suggest you using JavaScript location.href instead.

参考

  • How to recognize Facebook User-Agent

这篇关于php重定向网址与og元标记(开放图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆