如何在静态页面上利用 Facebook 本地化
How to take advantage of facebook localisation on static page
Facebook 希望您使用 og:locale:alternate
元标记来声明您的网站可用的语言。不幸的是,当他们想使用替代本地时,他们没有在标记中引用 &fb_locale=es_ES
到 URL 的末尾(或者他们想要的任何语言代码)。我的问题是我的网站是静态的,所以我无法轻松阅读这些额外信息,所以如果 .htaccess 文件中有一种方法可以重新映射 url.
所以
(.*)(?|&)fb_locale=(.*)
到 [first 2 letters of ]/[everything but first 2 letters of ]
所以
en/test.html?fb_locale=es_ES
应该去
es/test.html
RewriteCond %{THE_REQUEST} /([^/]+)/(.*)[\?&]fb_locale=([^_]+)_(.*) [NC]
RewriteRule ^ /%3/%2 [NC,L,R]
几乎可以工作,但出于某种原因, ? 之后的值?仍然被追加。
您可以在 root/.htaccess 中使用以下代码:
RewriteEngine On
#if the requested url is /en/test.html?fb_locale=foo_bar
RewriteCond %{THE_REQUEST} /en/test\.html\?fb_locale=([^_]+)_[^\s]+ [NC]
#Then redirect the request to /es/test.html?fb_locale=foo
RewriteRule ^ /es/test.html?fb_locale=%1 [NC,L,R]
尝试:
RewriteCond %{THE_REQUEST} /([^/]+)/([^?&]+)[\?&]fb_locale=([^_]+)_([^\s]+) [NC]
RewriteRule ^ /%3/%2? [NC,L,R]
末尾的空问号很重要,因为它会丢弃原始查询字符串。
在最后一个捕获组 ([^\s]+) 在 THE_REQUEST RewriteCond 中,我们排除匹配的空格 \s 并告诉正则表达式匹配有限的字符,因为正则表达式是贪婪的,它试图捕获请求字符串的整个剩余部分 spaceHTTP/1.1 .
这里有一个 The_Request 字符串的例子:
GET /en/index.html?q=foo HTTP/1.1
正确的正则表达式模式没有捕获组来匹配它:
/en/index\.html\?q=foo
带有正则表达式捕获组的模式:
/([^/]+)/([^?]+)\?q=([^\s]+)
第一个捕获组捕获 /en ,第二个捕获 /index.html ,第三个捕获组捕获值关键。
Facebook 希望您使用 og:locale:alternate
元标记来声明您的网站可用的语言。不幸的是,当他们想使用替代本地时,他们没有在标记中引用 &fb_locale=es_ES
到 URL 的末尾(或者他们想要的任何语言代码)。我的问题是我的网站是静态的,所以我无法轻松阅读这些额外信息,所以如果 .htaccess 文件中有一种方法可以重新映射 url.
所以
(.*)(?|&)fb_locale=(.*)
到 [first 2 letters of ]/[everything but first 2 letters of ]
所以
en/test.html?fb_locale=es_ES
应该去
es/test.html
RewriteCond %{THE_REQUEST} /([^/]+)/(.*)[\?&]fb_locale=([^_]+)_(.*) [NC]
RewriteRule ^ /%3/%2 [NC,L,R]
几乎可以工作,但出于某种原因, ? 之后的值?仍然被追加。
您可以在 root/.htaccess 中使用以下代码:
RewriteEngine On
#if the requested url is /en/test.html?fb_locale=foo_bar
RewriteCond %{THE_REQUEST} /en/test\.html\?fb_locale=([^_]+)_[^\s]+ [NC]
#Then redirect the request to /es/test.html?fb_locale=foo
RewriteRule ^ /es/test.html?fb_locale=%1 [NC,L,R]
尝试:
RewriteCond %{THE_REQUEST} /([^/]+)/([^?&]+)[\?&]fb_locale=([^_]+)_([^\s]+) [NC]
RewriteRule ^ /%3/%2? [NC,L,R]
末尾的空问号很重要,因为它会丢弃原始查询字符串。
在最后一个捕获组 ([^\s]+) 在 THE_REQUEST RewriteCond 中,我们排除匹配的空格 \s 并告诉正则表达式匹配有限的字符,因为正则表达式是贪婪的,它试图捕获请求字符串的整个剩余部分 spaceHTTP/1.1 .
这里有一个 The_Request 字符串的例子:
GET /en/index.html?q=foo HTTP/1.1
正确的正则表达式模式没有捕获组来匹配它:
/en/index\.html\?q=foo
带有正则表达式捕获组的模式:
/([^/]+)/([^?]+)\?q=([^\s]+)
第一个捕获组捕获 /en ,第二个捕获 /index.html ,第三个捕获组捕获值关键。