Maxim Lott Maxim Lott - 1 month ago 7
HTTP Question

Create 301 redirect that excludes facebook

I have a website that I changed from http to https. It has a 301 redirect to make sure everyone goes to the https version.

Unfortunately, that causes Facebook to see it as a different website and reset the share count to zero. In Facebook's developer guide, they give a solution to this:


if your new URL was "https ://example.com/new-url", and the old URL was
https://example.com/old-url, you should include this snippit in the
new-url:

meta property="og:url" content="https://example.com/old-url" /


HOWEVER, the fact that I have a 301 redirect on the site makes this not work. Facebook's crawler never reaches the http site. Here's what Facebook says about that:


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.


Does anyone have an idea how to do that? I can edit my .htaccess file. But what I can I put in there that would accomplish this?

Thank you!

EDIT: My current .htaccess file is simply:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Answer

It says you need to prevent the crawler/user agent from redirecting. So just add it to the rules.

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

So if https is not on and it's not the facebook crawler then redirect to https otherwise it will stay http.

Info here.

https://developers.facebook.com/docs/sharing/webmasters/crawler#updating