HTTP to HTTPS - Retain Facebook Likes and Share Count

If you have recently moved your blog from 'HTTP' to 'HTTPS', you would have noticed that your Facebook likes/share count is not automatically carried over.

So all your pages will have zero likes/shares. It's like going back to square one.

So what's the solution? How do you get back your facebook likes?

Well, there is a solution and it is pretty straight forward.

You need to add a 'OG URL' tag and point the tag to the 'HTTP' URL of your blog.

Example: Let's say the 'HTTPS' URL of a page is:

https://sitename.com/article-one/

The og:url tag for this page should look as follows:

<meta property='og:url' content='http://sitename.com/article-one/'>

As you would have noticed, the og:url tag has the 'HTTP' url of the page and not HTTPS.

What is the 'OG URL' Tag?

The OG URL (og:url) tag is a tag used by Facebook to grab the URL of a page. It credits links/share count to this URL. OG stands for Open Graph.

Chances are that if you have a SEO plugin (or a plugin that adds OG tags), then you already have this tag on your site.

To check if you have this tag, simply view the 'HTML source' of one of your blog posts. You can check the HTML source of a webpage by clicking the keys 'CTRL + U' (Command + U for mac) on your keyboard.

Once there, look within the <head> tags to find all your OG tags. Refer image below:

HTML source to check og tags

Steps for getting back FB Social Share Counts by changing the OG:URL tag to 'HTTP'

As mentioned earlier, most SEO plugins will add the OG:URL tag to your posts/pages. The issue is that the 'OG URL' tag will automatically point to the 'HTTPS' version of your webpage.

What we need to do is, point the og:url tag to the 'HTTP' version cause this is the version that has all our Facebook likes. For more information on this, read this documentation by Facebook: https://developers.facebook.com/docs/sharing/webmasters/crawler/

Once the og:url tag is changed, Facebook will re-attribute all like/share counts back to your posts/pages once it re-crawls your pages.

The re-crawling happens whenever someone new likes your posts/pages.

You can also force Facebook to re-crawl your webpages.

Here are the steps to do this in detail:

Step 1: Change your og:url tag to output 'HTTP' version of the webpage. (We will see how to do this later in this article.)

Step 2: Login to your Facebook account and go to Facebook OG Tags debugger here: https://developers.facebook.com/tools/debug/

Step 3: Enter the 'HTTPS' URL of any one of your website's posts/pages in the space given and click 'Debug'.

Step 4: If you get the "This URL hasn't been shared on Facebook before." message, click on the 'Fetch New Information' button.

Step 5: Next click 'Scrap Again' button to force Facebook to re-crawl your webpage.

Step 6: You should now be able to see all your likes re-attributed back to the page. You would also see two URLs under the 'Redirect path' heading which reflects your og:url meta tag and your original HTTPS URL as shown in the image below:

Facebook OG tags debugger

Step 7: Of-course it won't be feasible to do this for every single URL. To do this for multiple URLs at a time, use the batch invalidator tool here: https://developers.facebook.com/tools/debug/sharing/batch/

Step 8: The final step is to allow Facebook crawler to crawl the older 'HTTP' version of the page.

Here's how to do that:

Chances are that you have redirected all 'HTTP' requests to 'HTTPS' using the .htaccess file.

You will need to edit your .htaccess redirect code to allow Facebook crawler to access the HTTP page without getting redirected.

You can easily do this by adding the following redirect condition to the .htaccess directive:

RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]

This line needs to be added right before the 'RewriteRule'.

An example is as follows:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_USER_AGENT} !facebookexternalhit/[0-9]
RewriteRule ^(.*)$ https://sitename.com/$1 [R=301,L]

This code will redirect all users from HTTP to HTTPS pages except Facebook crawlers.

Note: Your redirect code might be different than the example above but that doesn't matter. Simply add the line before the 'RewriteRule' as mentioned earlier.

How to change OG URL tag?

Now let's see how you can change the 'OG URL' tag for all your web pages from HTTPS URLs to HTTP URLs.

Option 1: Changing OG:URL from HTTPS to HTTP using a plugin

Chances are that you are currently using a SEO plugin or a social media plugin that generates the 'OG URL' tag.

If that's the case, you can consider using a plugin to change the 'OG URL' output.

I developed the Change OG URL to HTTP plugin that can help you automatically change the OG:URL output for the following plugins:

  • Yoast SEO Plugin.
  • ALL In One SEO Pack.
  • WP facebook open graph protocol by Chuck Reynolds.
  • Facebook Open Graph, Google+ and Twitter Card Tags plugin by Webdados.

Here is the plugin: https://wordpress.org/plugins/change-og-url-to-http/

You can install the plugin by logging into your wordrpess dashboard (admin panel) and then clickling on 'Plugins > Add New' from the left panel. Search for 'Change OR URL to HTTP by Mukesh Mani' and you should be able to see the plugin. Click on 'Install' and then 'Activate'.

Check the HTML source of your pages to see if changes have taken effect.

Here's an example of what the plugin does:

Change og url to http plugin output

Note: Note that this plugin changes the OG:URL only for posts that existed when installing the plugin. All new posts that you add will have the regular OG:URL tag with HTTPS.

Note 2: Make sure to give facebook crawlers access to HTTP version of your webpages for this to work. Read previous heading in this article to know how to do that.

Option 2: Changing OG:URL from HTTPS to HTTP Manually

If you do not want to use the plugin, here's how you can do this manually.

If you are not using any SEO/Social Media plugin, you can add the following code to your theme's functions.php file to add a 'OG URL' tag with a HTTP:

Note: If you are using Yoast, AllinSEO or any other social media plugin, scroll further down to see what code to use.

function add_http_og_url_tag(){
if( ! is_singular() ) return;
  global $post;
  $ogurl = get_permalink( $post->ID );
  $ogurl = esc_url( str_replace( 'https://','http://',$ogurl ) );
  /* Output OG:URL tag with HTTP */
  echo "\n<meta property='og:url' content='$ogurl'>\n";
}
add_action('wp_head', 'add_http_og_url_tag');

Applying the code only to existing posts and not new posts

Even though the above code will work, as you would have realized by now, there is another issue. The issue is that the above code will add the 'OG URL' tag with 'HTTP' to the existing posts and also the new posts. In other words, all the new posts that you will be adding.

We of-course do not want to keep adding the 'HTTP' URLs for new posts.

To resolve this, we can do a date comparison and then add the tags only to older posts/pages.

Say for example, you shifted to 'HTTPS' on '2nd of August 2017'. You can compare dates to stop adding OG URL tag with 'HTTP' for all posts up till '2nd of August 2017' and then go back to adding the regular 'OG URL' tag with the 'HTTPS' urls for all the newer posts.

Here's a function that does that for you:

function add_http_og_url_tag(){
if( ! is_singular() ) return;
global $post;
/* date to stop adding HTTP OG URL tags */
$ow_date_to_stop = 'August 2, 2017';
$ow_date_to_stop = strtotime( $ow_date_to_stop );
/* Current post date */
$ow_cur_post_date = esc_html( get_the_date() );
$ow_cur_post_date = strtotime( $ow_cur_post_date );
/* Current post URL */
$ogurl = get_permalink( $post->ID );
if( $ow_cur_post_date < $ow_date_to_stop  ){
  $ogurl = esc_url( str_replace( 'https://','http://',$ogurl ) );
}
  echo "\n<meta property='og:url' content='$ogurl'>\n";
}
add_action('wp_head', 'add_http_og_url_tag');

Now, the above code will only add 'HTTP' version of 'OG URL' tag for posts older than the set date.

Note: Make sure to change the date in the code from August 2, 2017 to any date that you want.

Now let's see how you can add a filter to the og:url output if you are using the following popular plugins that add OG tags to your site:

Yoast SEO plugin

If you are using Yoast SEO plugin, you can add the following code to your theme's functions.php file, to make the required changes:

function ow_ogurl_yoast_filter( $ogurl ){

/* date to stop adding HTTP OG URL tags */
$ow_date_to_stop = 'August 2, 2017';
$ow_date_to_stop = strtotime( $ow_date_to_stop );

/* Current post date */
$ow_cur_post_date = esc_html( get_the_date() );
$ow_cur_post_date = strtotime( $ow_cur_post_date );	

if( $ow_cur_post_date < $ow_date_to_stop  ){
  return str_replace( 'https://','http://',$ogurl );
}

return $ogurl;
}
add_filter( 'wpseo_opengraph_url', 'ow_ogurl_yoast_filter', 10, 1 );

Note: Don't forget to change the date in the code from 'August 2, 2017' to any date you want.

All In One SEO Pack (AISEO) plugin

If you are using the 'All In One SEO Pack' plugin, then add the following code to your theme's functions.php file to make this work:

function ow_ogurl_aiseo_filter( $value, $type, $field ){
	
/* date to stop adding HTTP OG URL tags */
$ow_date_to_stop = 'August 2, 2017';
$ow_date_to_stop = strtotime( $ow_date_to_stop );

/* Current post date */
$ow_cur_post_date = esc_html( get_the_date() );
$ow_cur_post_date = strtotime( $ow_cur_post_date );	

if( $ow_cur_post_date < $ow_date_to_stop ){
if( $field == 'url' ){
	$value = str_replace( 'https://','http://',$value );
	return $value;
}}
return $value;
}
add_filter('aiosp_opengraph_meta', 'ow_ogurl_aiseo_filter', 10, 3);

Note: As usual, make sure to change the date in the code before using.

Facebook Open Graph, Google+ and Twitter Card Tags plugin by Webdados

If you are using the Facebook Open Graph plugin by Webdados, then use the following code:

function ow_ogurl_fb_filter( $ogurl ){

/* date to stop adding HTTP OG URL tags */
$ow_date_to_stop = 'August 2, 2017';
$ow_date_to_stop = strtotime( $ow_date_to_stop );

/* Current post date */
$ow_cur_post_date = esc_html( get_the_date() );
$ow_cur_post_date = strtotime( $ow_cur_post_date );	

if( $ow_cur_post_date < $ow_date_to_stop ){
    return str_replace( 'https://','http://',$ogurl );
}
return $ogurl;
}
add_filter('fb_og_url', 'ow_ogurl_fb_filter');

WP Facebook Open Graph protocol by Chuck Reynolds

If you are using the WP Facebook OGP plugin by Chuck Reynolds, then use the following code:

function ow_ogurl_wpfbogp_filter( $ogurl ){

/* date to stop adding HTTP OG URL tags */
$ow_date_to_stop = 'August 2, 2017';
$ow_date_to_stop = strtotime( $ow_date_to_stop );

/* Current post date */
$ow_cur_post_date = esc_html( get_the_date() );
$ow_cur_post_date = strtotime( $ow_cur_post_date );	

if( $ow_cur_post_date < $ow_date_to_stop ){
    return str_replace( 'https://','http://',$ogurl );
}
return $ogurl;	
}
add_filter('wpfbogp_url','ow_ogurl_wpfbogp_filter');

Will doing this impact SEO negatively?

The OG:URL tag is only used by Facebook to determine the URL of the page. Google uses the 'Canonical tag' instead. So as long as your 'Canonical tag' points to the HTTPS version of your site, you are good to go.

So no, you won't be impacted negatively from the SEO perspective.

 
 
 
 

Comments

  1. Hello

    Thanks for this article, pls any code to restore my social share using this your method though I changed permalink from /%year%/%monthnum%/%postname%.html to /%postname%.html pls help

    I may have to also contact you to check out my site SEO cause for the past few months I have lost 90% of my traffic i meant from 12kpv daily to 800pv pls help

  2. Henning says:

    Hi there,

    I am currently in the process of migrating a couple of sites to HTTPS and your plugin / post is a godsend!! Thanks for providing all the infos, very much appreciated.

    Unfortunately Facebook doesn't recognize or accept the different og:url setting and outputs the canonical URL as HTTPS-version (although the scraped info correctly shows the og:url being set to HTTP).

    Any ideas on why this might happen and how to fix this? You see the issue when using the sharing debugger on this URL: https://www.martin-von-bergen.info/warum-maenner-sich-emotional-distanzieren/er-zieht-sich-zurueck-obwohl-er-frueher-liebevoll-um-sie-bemueht-war

    Thanks & regards!

    P.S.: I'd love to subscribe to the comment section here to be notified but since that's not possible I will check back in a day or two...

    • M Mukesh says:

      Hi Henning,

      I figured out what the issue is. The issue happens when you don't allow facebook to access the HTTP version of your webpages. You need to make changes in your HTaccess file to allow HTTP access only to facebook crawlers. I made the required changes in the article on how to do this.

  3. Henning says:

    Nevermind my earlier comment! I found the culprit: The HTTP redirect was outweighing the og:url setting, took some fiddling to sort it all out but it's working fine now.

    Thanks again!!

Leave a Reply

Your email address will not be published. Required fields are marked *