如何使用 hashbang url 处理 Facebook 共享/喜欢? [英] How to handle facebook sharing/like with hashbang urls?
问题描述
我正在构建一个网站,我将从主页打开网站上的其他一些 URI 到灯箱 (AJAX) 中,并且我想使用 HTML5 推送状态和哈希刘海作为后备来管理状态更改.>
现在我希望网址可抓取且可在 Facebook 上分享/喜欢..
如果用户浏览器支持 HTML5 推送状态,没问题,他可以分享 URL(例如:http://myserver/example
),Facebook 会在静态中找到合适的 OG metas内容.
但如果用户使用 HTML4 浏览器,他将拥有一个类似 http://myserver/#!/example
的 url.我希望他无论如何都能通过 Facebook 分享它......
现在看起来 Facebook 支持 _escaped_fragment_ 替换方法,所以我虽然我只想重定向来自 http://myserver/?_escaped_fragment_=/example
的请求http://myserver/example
每个人都应该开心......
所以我在我的 htaccess 中添加了一个重写条件:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=([^&]*)重写规则 .* http://%{HTTP_HOST}/%1?[R=301,L,NE]
我的问题是我不能让它与 Facebook 一起工作,使用 Facebook linter 它似乎一直在百分比转义 hashbang 之后的 URL 部分,导致像这样的 URLhttp://myserver/%2Fexample
出现 404 :-(
有人知道如何欺骗 Facebook 不转义 URL 的这一部分吗?我可以在 apache mod_rewrite 方面做些什么吗?
我也愿意接受任何其他有效的 ajax 可抓取/喜欢的 URL 策略;)
我结束了使用 php header() 重定向,效果很好:
if(isset($_GET['_escaped_fragment_'])) {Header("HTTP/1.1 301 永久移动");header('位置:http://'.$_SERVER['HTTP_HOST'].$_GET['_escaped_fragment_']);死();}
我不知道为什么在使用 htaccess 重写条件时会出现转义字符.
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 ;)
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.
这篇关于如何使用 hashbang url 处理 Facebook 共享/喜欢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!