Thursday, September 9, 2010

The Facebook Like button PITA: fb_xd_fragment=

So I have been using Facebook's like button for awhile now. I have it on a couple blogs, and a site I maintain using drupal. I started noticing that I was getting requests with a query of "?fb_xd_fragment=". This is a problem, because it shows a blank page.

I did some investigating, and this seems to be caused by two bugs (that I can tell). One, that Facebook's API causes a bug in IE that sends this query in the first place, and second that my website, because of the Facebook API, causes the page to become visibly blank because of it.

So, what to do? I decided to kill the query string at the source: my apache web server using mod_rewrite.

So here's what I added to the apache config for just this web server:

RewriteEngine On
RewriteCond %{QUERY_STRING} fb_xd_fragment=
RewriteRule (.*) $1? [R=301]

So this basically sends a 301 HTTP code (Moved Permanently) with the same request, minus the query string. Note that the "?" on the RewriteRule is needed so that mod_rewrite doesn't append the old query back on. Seems to be doing the trick, and now I'm not losing traffic due to a Facebook API bug.

10 comments:

  1. Hi Ben, check this blog post. It explains where the problem is coming from and suggests ways to fix it without changing your server settings: How to fix the Like button bug

    ReplyDelete
  2. Thanks Stephan. I did in fact do a few of the things suggested in order to fix things up, but I prefer keeping the apache mod_rewrite just o make sure the request gets to a working page. As you know, even a few missed visitors can cost you money :)

    ReplyDelete
  3. Good one, I've been wondering where this came from and couldn't reproduce it (no IE around).

    For those who can't use mod_rewrite, the same results can be created in PHP with the following snippet of code:

    if ( array_key_exists( 'fb_xd_fragment', $_GET ) ) {
    header( "Location: ". $_SERVER['SCRIPT_URL'] );
    exit();
    }

    Put it on top of your page (or init script, or whatever).

    Of course even better would be to only unset the fb_xd_fragment path and redirect with the rest of the query string intact.

    ReplyDelete
  4. Oh, something I forgot: in my example you'll obviously want

    header( 'HTTP/1.1 301 Moved Permanently' );

    as well, to instruct search engines to ignore the URI containing the offending query string.

    ReplyDelete
  5. Ben may I ask you something? Your solution is perfect if you can access the configuration file of your web server, but what do you suggest for people like me using blogger?

    thanks

    ReplyDelete
  6. We have written up a little tutorial on how to fix the problem: http://www.siliconbeachtraining.co.uk/free-resources/strange-code-google-analytics-training/
    this will fix both the duplicate content problem and the Google Analytics problem

    ReplyDelete
  7. Hi,

    Your code is redirecting ok, but appending the server path to the vhost:


    http://www.server.pt/index.php?fb_xd_fragment=

    redirects to

    http://www.server.pt/sites/internos/directobras/html/index.php

    any thoughts?

    thanks for any help,

    Gustavo Melo

    ReplyDelete
  8. Hi,

    Your code is redirecting, but appending the complete server path of the vhost to the url:

    http://www.server.pt/index.php?fb_xd_fragment=

    redirects to

    http://www.server.pt/sites/internos/directobras/html/index.php

    any thoughts?

    thanks for any help,

    Gustavo Melo

    ReplyDelete
  9. Hi Ben

    Thanks for this. I like your solution - one question.

    Forgive me if I have misunderstood the source of the problem at some level but I am working in "panic mode" since I discovered this in my Analytics and want to move fast to avoid SEO penalties.

    It is my understanding that some versions of IE are redirecting on pages with like buttons to the same page with the fb_xd_fragment variable appended.

    If that is the case, won't an apache redirect send them into a never-ending loop?

    Thanks
    James

    ReplyDelete
  10. Hi Ben, check this blog post too, i've found another URL different form the standard fb_xd_fragment URL that we have been seeing. It also takes visitors to a blank page and requires an additional 301 redirect. fd_xd_fragment

    - Alec Gordon

    ReplyDelete