Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

facebook - php redirect url with og metatag (open graph)

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

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

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"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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');
}

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.

Reference


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...