Search code examples
ajaxfacebookmod-rewritepushstatehashbang

How to handle facebook sharing/like with hashbang urls?


I am building a website where from the homepage i will open some other URI on the website into a lightbox (AJAX), and i want to use HTML5 push state and hash bangs as a fallback to manage changes of states.

Now i want the urls to be crawlable and Facebook shareable/likeable..

If the user browser supports HTML5 push state, no problem, he can share the URL (for example : http://myserver/example ) and Facebook will find the appropriate OG metas in the static content.

But if the user uses a HTML4 browser, he will have a url like http://myserver/#!/example. and i want him to be able to share it through facebook anyway...

Now it looks like Facebook supports the _escaped_fragment_ replacement method, so i though i would simply redirect requests from http://myserver/?_escaped_fragment_=/example to http://myserver/example and everybody should be happy…

So i added a rewrite condition to my htaccess :

RewriteCond %{QUERY_STRING} ^_escaped_fragment_=([^&]*)
RewriteRule .* http://%{HTTP_HOST}/%1? [R=301,L,NE]

My problem is that i can't make it work with Facebook, with Facebook linter it seems to percent-escape all the time the part of the URL after the hashbang, resulting in urls like http://myserver/%2Fexample which lands to a 404 :-(

Does anybody knows how to trick Facebook into not escaping this part of the URL ? Can i do something on the apache mod_rewrite side ?

I am also open to any other valid ajax crawlable/likeable URL strategy ;)


Solution

  • i ended using a php header() redirect, which works perfectly :

    if(isset($_GET['_escaped_fragment_'])) {
        Header( "HTTP/1.1 301 Moved Permanently" );
        header('Location: http://'.$_SERVER['HTTP_HOST'].$_GET['_escaped_fragment_']);
        die();
    }
    

    I don't know why i got escaped characters when doing it with the htaccess rewrite condition.