如何检测社交网络并重定向到PHP以生成元标记

时间:2017-01-27 13:47:10

标签: php apache .htaccess redirect http-redirect

我有一个托管在

的网站

示例:http://lab.example.com/app

当像facebook / twitter这样的社交网络机器人试图抓住这个网站时,我需要它重定向到生成正确元标记的php文件。我尝试以几种方式修改HTACCESS,但经过多次研究,我无法让Facebook / Twitter调试器阅读它。

的.htaccess

RewriteEngine On
RewriteBase /

RewriteRule ^index\.html$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . app/index.html [L]

# allow social media crawlers to work by redirecting them to a server-rendered static version on the page
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule album/(\d*)$ http://lab.example.com/app/opengraph.php [P]

OPENGRAPH.PHP TEST

<?php


makePage();      

function makePage() {
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <meta property="og:title" content="Title Test" />
            <meta property="og:description" content="Description Test" />
            <meta property="og:image" content="http://68.media.tumblr.com/784fb357523d75590261ba6c5c19e6e7/tumblr_o8punbn6Qo1st5lhmo1_1280.jpg" />
            <!-- etc. -->
        </head>       
    </html>
    <?php
}

?>

希望有人有经验的HTACCESS可以帮助我

1 个答案:

答案 0 :(得分:0)

To provide an answer, not what you asked, but what you are trying to achieve.

"The OG Metatags will be on every page on Index.html, it is just I need it to be different when facebook/twitter tries to scrape it."

Twitter has its own set of og tags ..

<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="">
<meta name="twitter:description" content="">
<meta name="twitter:image" content="">

As for facebook, all you need to do, is detect the useragent.

<?php
if(strpos($_SERVER['HTTP_USER_AGENT'],'facebookexternalhit')!==false){
 //Do Facebook Og
}
else{
 // Do basic Og
}
?>