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 older '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.

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

Getting back FB Likes/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 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/plugins/faqs#moving-urls

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/

Changing OG:URL from HTTPS to HTTP using a plugin

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.

Code to change OG URL Tag 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 plugin, you can add the following code to your theme's functions.php file to add a OG URL tag with a HTTP:

function add_http_og_url_tag(){
if( ! is_singular() ) return;
global $post;
$ogurl = esc_url( get_permalink( $post->ID ) );
$ogurl = explode( ':', $ogurl );
$ogurl = 'http:' . $ogurl[1];
/*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 this 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 = esc_url( get_permalink( $post->ID ) );

if( $ow_cur_post_date < $ow_date_to_stop  ){
$ogurl = explode( ':', $ogurl );
$ogurl = 'http:' . $ogurl[1];	
}

echo "\n<meta property='og:url' content='$ogurl'>\n";
}

/* Add og url tag to wp head section */
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  ){
$ogurl = explode( ':', $ogurl);
$ogurl_part = $ogurl[1];
$ogurl = 'http:' . $ogurl_part;	
}

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' ){
	$ogurl_array = explode( ':', $value );
	$ogurl_part = $ogurl_array[1];
	$value = 'http:'.$ogurl_part;
	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 ){
	$ogurl_array = explode( ':', $ogurl );
	$ogurl_part = $ogurl_array[1];
	$ogurl = 'http:'.$ogurl_part;
	return $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 ){
	
	$ogurl_array = explode( ':', $ogurl );
	$ogurl_part = $ogurl_array[1];
	$ogurl = 'http:'.$ogurl_part;
	return $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.

 
 
 
 

Leave a Reply

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