【问题标题】:How to detect Social Network and Redirect to PHP to generate Metatags如何检测社交网络并重定向到 PHP 以生成元标记
【发布时间】:2017-01-27 13:47:10
【问题描述】:

我有一个托管在

的网站

示例: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 测试

<?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 经验的人可以帮助我

【问题讨论】:

  • 以最安全的方式为每个请求生成这些元标记。您无法预测现有和未来的机器人将使用哪个用户代理。
  • Google 真的,真的,真的不赞成人们向他们的机器人展示与普通用户不同的东西。
  • 您应该在每个 pagwe 上包含 OG 元标记,无论它请求的是谁。
  • 为每个请求生成元标记是什么意思?我目前正在尝试避免使用任何预渲染服务 - 由于 Facebook/Twitter 在页面加载后不会读取 javascript 生成的元标记,这就是我尝试走这条路的原因。无论如何,我可能会删除 Google,因为 Google 与单页应用程序配合得更好。
  • OG Metatags 将出现在 Index.html 的每个页面上,只是当 facebook/twitter 试图抓取它时我需要它有所不同。

标签: php apache .htaccess redirect http-redirect


【解决方案1】:

提供答案,不是你问什么,而是你想要达到什么。

“OG Metatags 将出现在 Index.html 的每个页面上,只是当 facebook/twitter 试图抓取它时,我需要它有所不同。”

Twitter 有自己的一组 og 标签 ..

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

对于 facebook,您需要做的就是检测用户代理。

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

【讨论】:

    猜你喜欢
    • 2012-04-08
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多