Facebook Sharing Debugger 在使用 CloudFlare Page Rule 重定向到 HTTPS 时出错

Facebook Sharing Debugger gives Error when using CloudFlare Page Rule to redirect to HTTPS

我有一个网站最近切换到了 HTTPS。为了将 HTTP 重定向到 HTTPS,我在 CloudFlare 中创建了一个页面规则:

http://www.domain.com/*
Always Use HTTPS

这有效,输入站点的 HTTP 版本会重定向到 HTTPS 版本。

然而,这一变化使网站失去了原始 HTTP 版本的 Facebook "Likes"。

为了解决这个问题,我遵循 Facebook's "moving URLs" instructions 并将 og:url 元标记添加到具有原始 HTTP 域的 HTTPS 站点以捕获原始 Likes:

<meta property="og:url" content="http://www.domain.com"/>

然而,当在 Facebook Sharing Debugger 上重新抓取网站时,Facebook 给我一个错误提示:

Could Not Follow Redirect Path
Using data from https://www.domain.com/ because there was an error following the redirect path.

Could Not Follow Redirect
URL requested a HTTP redirect, but it could not be followed.

我该如何解决这个问题?

Facebook's docs 指出

... the old URL still renders a document with Open Graph tags and returns a HTTP 200 response, at least when loaded by Facebook's crawler. If you want other clients to redirect when they visit the URL, you must send your 301 HTTP response to all non-Facebook crawler clients. The old URL should contain its own og:url tag that points to itself.

即,http://www.domain.com/ 仍然需要 Facebook 爬虫可以访问,因此在 CloudFlare 级别重定向到 HTTPS 可能会出现问题。

您可以尝试在站点的根目录下创建一个 .htaccess 文件,而不是在 CloudFlare 重定向到 HTTPS:

RewriteEngine On
RewriteCond %{HTTPS} !^on
RewriteCond %{HTTP_USER_AGENT} !(Facebot|facebookexternalhit/1.1) [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

如果有效,Facebook 爬虫将看到您网站的 HTTP 版本,其他所有人将被重定向到 HTTPS

祝你好运!