Fixing facebook like button error

Here is the story: Assuming that you want to have like button for your website in order to promote its pages. Meanwhile you do know that your website encoding is not UTF-8. You go straight to facebook.com to get and install the proper javascript or iframe code.
So far so good, the button is ready and shows up on your pages. But when you click on it gives out the nasty error stating:
The page http://www.your_website.com/your-web-page.html cannot be reached:
After hours of struggling with this problem you are determined either to remove the button or to fix it somehow.

Here is my solution that works wonderfully with cyrillic URLS ( using windows-1251 encoding ) assuming that you use php place this piece of code at the beginning of your web page:
<?php
$page=urldecode($_SERVER['REQUEST_URI']); 
$page=iconv("UTF-8", "windows-1251", $page);

if(mb_detect_encoding($page, 'ASCII, UTF-8, ISO-8859-5')=="UTF-8")
{
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://{$_SERVER["SERVER_NAME"]}$page"); 
exit;
}
?>

Explanation is simple: First we detect if the requested url comes from facebook or other UTF-8 encoded website. In such case we redirect(refresh) the whole webpage but this time properly encoded using our preferred windows-1251 encoding. After the redirect the url will be recognized by our system as a valid one. NB: the order of encodings passed to mb_detect_encoding.
If you've been using encoding different than windows-1251 then just replace it in the iconv function like so:
$page=iconv("UTF-8", "your_custom_encoding", $page);
 
Cheers! by Nevyan Neykov



0 коментара:

Post a Comment