0

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+'&amp;src=sdkpreparse"><span id="fb_span"><i class="fa fa-facebook-official" aria-hidden="true" style="color: white"></i> Share </span></a></div>
Community
  • 1
  • 1
Abhishek Sinha
  • 5,095
  • 1
  • 15
  • 14
  • I’d say, first of all you should think about whether it makes much sense to share stuff that is behind a login in the first place. At least for normal timeline posts it doesn’t, IMHO. People who are friends with your user who shared something will see it in their newsfeed – but they will _not_ get the actual content when they follow the link, but will be redirected to your login page only – extremely frustrating user experience! (It might make sense in _some_ situations, like f.e. when sharing happens mostly inside a group, where all members are registered users of your site as well.) – CBroe Aug 01 '16 at 10:06
  • I agree to your point but this is kind of a polling site so that's why I've added social media plugins. Nevertheless, I think I have found the problem and its none of the above listed ones. I found that my htaccess is not even being read (hence the FB never passes the login page). After surfing more, I found that its a Godaddy problem and many other users have experienced same weird problems with their htaccess file. I don't know much about htaccess stuff but I'm working on it. – Abhishek Sinha Aug 02 '16 at 07:34

1 Answers1

0

In case og:url is specified, Facebook tries to fetch meta tags from the link specified in or:url. It treats it as a canonical URL as mentioned here : https://developers.facebook.com/docs/sharing/webmasters/getting-started/versioned-link