I researched a lot about FB share issues but yet haven't resolved mine. I think I even have found the problem in these posts- https://stackoverflow.com/a/26714882/5348972 https://stackoverflow.com/a/35653463/5348972
My website contains a basic login system where if session is set -> display page else redirect to login page. So when I provide static og data to <head>
, example-
<meta property="og:image"content="http://example.com/images/loginPageCarausel/unnamed.png" />
<meta property="og:title" content="Some Title" />
<meta property="og:url" content="http://example.com" />
<meta property="og:description" content="No Description" />
<meta property="og:image:type" content="image/jpeg" />
<meta property="og:image:width" content="400" />
<meta property="og:image:height" content="300" />
Everything works fine. Post is shared with specified image because this part of <head>
is common whether session is set or unset. However, when I change the header to this,
<?php
include_once 'includes/functions.php';
$pg = getPageName('pages.php');
if($pg) {
if(isset($_GET['poll'])) {
$i = $_GET['poll'];
if(class_exists('Polls') && class_exists('FilesUpload')) {
$vip_poll = Polls::getPollDataById(POST_TBL, $i);
$title = $vip_poll->question;
$gen = $vip_poll->poll_genre;
$vip_pollOwnersId = $vip_poll->user_id;
$media_files = FilesUpload::getMediaFiles(MED_TBL, $i, $vip_pollOwnersId);
$f_type = $media_files->file_type;
$f = $media_files->file_name;
$img_ext_array = array('jpg', 'gif', 'jpeg', 'png');
if (in_array($f_type, $img_ext_array)) {
$analysis = Polls::select_all_from_graph_page_data(GP_DATA_TBL, $vip_pollOwnersId, $i);
if($analysis != false) {
if (!empty($analysis)) {
$analysis_data = $analysis->data;
if (strlen($analysis_data) > 300) {
$analysis_data = substr($analysis_data, 0, 300) . "...";
}
$analysis = $analysis_data; ?>
<meta property="fb:app_id" content="1775**************">
<meta property="og:image" content="http://www.example.com/<?=$f?>" />
<meta property="og:title" content="<?=$title?>" />
<meta property="og:url" content="http://example.com/pages.php?vip_poll=<?=$i?>&genre=<?=$gen?>" />
<meta property="og:description" content="<?=$analysis_data?>" />
<meta property="og:image:type" content="image/jpeg" />
<meta property="og:image:width" content="400" />
<meta property="og:image:height" content="300" />
<?php }
}
}
}
}
}
?>
In this case FB crawler finds one of the image of login page and FB debugger issues various og properties missing. I suspect this happens because the ajax request pages redirects FB crawler to login page because its session is never set. I have already read this page
https://developers.facebook.com/docs/sharing/webmasters/crawler
and tried solving this issue with HTACCESS and added this few rules in my htaccess file
RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit
RewriteRule ^(.*)$ ogtags.php?$1 [L,QSA]
But it did not helped me either. I don't know much about htaccess coding but I wanted to know can these lines let crawler access contents even when its not logged in. If not then do I need to change my login logic (which perhaps is not a good idea). If there's better ways please elaborate.
I also tried changing the meta tags using jquery but that didn't worked either.
My question is how do I redirect FB crawler to the correct url specified which is <meta property="og:image" content="http://www.example.com/<?=$f?>" />
? I can also redirect crawler to another page through htaccess
RewriteCond %{HTTP_USER_AGENT} facebookexternalhit [NC,OR]
But then I would need to send post id to run queries and update meta tags dynamically in that redirected page. I don't know how that can be done (sending an id to two different urls at same time). My second question is I read in FB that it scrapes info only when the post is shared. I tried to change the meta tags both by jquery and php and shared the content when the console showed all the og tags correctly, but the crawler still went to login page (the unset html page)? This is the fb share code I'm using-
<div class="fb-share-button" data-href="http://www.example.com/pages.php?vip_poll='+app_id+'" data-layout="button_count" data-mobile-iframe="true"><a class="fb-xfbml-parse-ignore" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u='+url_enc+'&src=sdkpreparse"><span id="fb_span"><i class="fa fa-facebook-official" aria-hidden="true" style="color: white"></i> Share </span></a></div>