Skip to content

Instantly share code, notes, and snippets.

@cherouvim
Last active May 13, 2021 16:56
Show Gist options
  • Save cherouvim/9e8721ef4a9589fd36e1c7a4be419fe9 to your computer and use it in GitHub Desktop.
Save cherouvim/9e8721ef4a9589fd36e1c7a4be419fe9 to your computer and use it in GitHub Desktop.
do not lose facebook likes when migrating your wordpress site from HTTP to HTTPS
# 1) Disable any automatic http to https redirect on your proxy or hosting provider. Both http and https traffic should reach your .htaccess so you control redirects from there.
# 2) .htaccess should redirect facebook's bot to http, and everyone else to https.
RewriteEngine on
RewriteCond %{REQUEST_SCHEME} ^https$
RewriteCond %{HTTP_USER_AGENT} facebookexternalhit/[0-9]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_SCHEME} ^http$
RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 3) header.php should render the og:url.
<meta property="og:url" content="<?=is_front_page() ? "http://www.example.com/" : str_replace('https://', 'http://', wp_get_canonical_url()) ?>" />
# 4) Options "WordPress Address (URL)" and "Site Address (URL)" at /wp-admin/options-general.php should start with https://.
# 5) Clear cache (if you are using a plugin).
# 6) Debug your frontpage and a couple of posts at https://developers.facebook.com/tools/debug/. Hit "Scrape Again" and watch the logs if necessary. Also debug using:
curl --silent --header "user-agent: facebookexternalhit/1" http://www.example.com/whatever-post/ | head -n 20
curl --silent --header https://www.example.com/whatever-post/ | head -n 20
# 7) facebook sucks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment