【发布时间】:2018-12-24 03:04:59
【问题描述】:
我正在使用 AngularJS 与 Django REST API 并行开发单页应用程序。
我想做什么?
我希望当用户在 facebook 上分享一些帖子时,所有元标记都被 facebook 爬虫(机器人)读取属性。
问题
AngularJS通过双向数据绑定动态生成其内容,Facebook爬虫无法正常读取。我尝试使用 prerender.io,但没有成功。这就是为什么我决定创建一个 django API 端点,其唯一目标是仅使用可共享的元标记呈现空白页面。如果用户到达那里,他将被重定向到正确的帖子。
DJANGO 代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Prerender</title>
<meta property="fb:app_id" content="MY_APP_ID_HERE">
<meta property="og:type" content="website">
<meta property="og:description" content="{{property.description}}">
<meta property="og:title" content="{{property.title}}">
<meta property="og:url" content="https://www.therentalmoose.com/property/{{property.id}}">
<meta property="og:image"
content="https://www.therentalmoose.com:8000/static/images/properties/{{property.id}}/0.jpeg">
<meta property="og:image:height" content="488">
<meta property="og:image:width" content="931">
</head>
<body>
<script>window.location.replace('https://www.therentalmoose.com/property/{{property.id}}');</script>
</body>
</html>
** 我正在尝试的解决方案(到目前为止没有成功)**
我正在尝试配置我的 .htaccess 文件以在向 https://www.therentalmoose.com/property/2 发出请求时重定向 facebook 爬虫机器人 到 https://www.therentalmoose.com:8000/prerender/property/2 这就是我的元标记信息正确配置的地方。请注意,“2”是一个动态变量,应根据每个请求进行更改。
到目前为止,我的 .htaccess 文件看起来像这样(它位于我服务器上的 /var/www/html)
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule ^property/([A-Za-z0-9-]+)$ http://www.therentalmoose.com:8000/prerender/property/$2 [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
#this condition for crawlers not to interfere with normal access
RewriteCond %{HTTP_USER_AGENT} !(facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet)
#this rule for html5 mode and friendly urls without the #
RewriteRule ^(.*)$ /#/$1 [L]
</ifModule>
错误*
在 facebook 调试器工具上尝试第一个 url 时,它会引发 404 错误。 https://developers.facebook.com/tools/debug/sharing/?q=https%3A%2F%2Fwww.therentalmoose.com%2Fproperty%2F2%2F
但是,如果我尝试直接抓取 API 预渲染端点,一切正常:
有人可以帮帮我吗?
到目前为止,我一直在尝试用许多不同的方法解决这个“简单”的问题。
提前谢谢你。
【问题讨论】: