For language redirects we currently create folders in the web root containing an index.php file which checks the HTTP_ACCEPT_LANGUAGE
server variable. e.g. for the url www.example.com/press/
对于语言重定向,我们当前在Web根目录中创建包含index.php文件的文件夹,该文件检查HTTP_ACCEPT_LANGUAGE服务器变量。例如为网址www.example.com/press/
in /var/www/site/press/index.php
:
在/var/www/site/press/index.php中:
<?php
if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "en")
header("location: ../press_en.php");
else
header("location: ../press_de.php");
?>
As the site has grown, we now have many such folders. I am trying to clean this up by moving the redirects to a single .htaccess file:
随着网站的增长,我们现在有很多这样的文件夹。我试图通过将重定向移动到单个.htaccess文件来清理它:
RewriteEngine on
# Set the base path here
RewriteBase /path/to/site/
# The 'Accept-Language' header starts with 'en'
RewriteCond %{HTTP:Accept-Language} (^en) [NC]
# EN redirects
RewriteRule press(/?)$ press_en.php [L,R]
# DE redirects (for all languages not EN)
RewriteRule press(/?)$ press_de.php [L,R]
The idea is the same as the php file, but it doesn't work. I have tried all the possible language settings / orders in Firefox preferences, and checked the headers are correct, but it always serves the press_de.php
file.
这个想法与php文件相同,但它不起作用。我在Firefox首选项中尝试了所有可能的语言设置/命令,并检查了标题是否正确,但它始终提供press_de.php文件。
What am I doing wrong, or is there a better way? (not including content negotiation / multiviews or anything that requires renaming files, this is not currently an option).
我做错了什么,或者有更好的方法吗? (不包括内容协商/多视图或任何需要重命名文件的内容,目前这不是一个选项)。
2 个解决方案
#1
6
I would put the language indicator at the start of the URL path like /en/…
or /de/…
. Then you can use a single script that checks the preferred language and redirects the request by prepending the language indicator:
我会将语言指示器放在URL路径的开头,如/ en / ...或/ de / ....然后,您可以使用单个脚本来检查首选语言,并通过预先添加语言指示符来重定向请求:
// negotiate-language.php
$availableLanguages = array('en', 'de');
if (!preg_match('~^/[a-z]{2}/~', $_SERVER['REQUEST_URI'])) {
$preferedLanguage = someFunctionToDeterminThePreferedLanguage();
if (in_array($preferedLanguage, $availableLanguages)) {
header('Location: http://example.com/'.$preferedLanguage.$_SERVER['REQUEST_URI']);
} else {
// language negotiation failed!
header($_SERVER['SERVER_PROTOCOL'].' 300 Multiple Choices', true, 300);
// send a document with a list of the available language representations of REQUEST_URI
}
exit;
}
And the corresponding rules:
和相应的规则:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ negotiate-language.php [L]
RewriteRule ^([a-z]{2})/([^/]+)$ $2_$1.php [L]
Note that you need a proper someFunctionToDeterminThePreferedLanguage
function as Accept-Language header field is not a single value but a list of qualified values. So there might be more than just one value and the first value is not always the prefered value.
请注意,您需要一个正确的someFunctionToDeterminThePreferedLanguage函数,因为Accept-Language标头字段不是单个值,而是一个限定值列表。因此,可能不仅仅有一个值,第一个值并不总是首选值。
#2
2
in htaccess
在htaccess
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (en) [NC]
RewriteRule .* server.com/press_en.php [L]
RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteRule .* server.com/press_de.php [L]
#1
6
I would put the language indicator at the start of the URL path like /en/…
or /de/…
. Then you can use a single script that checks the preferred language and redirects the request by prepending the language indicator:
我会将语言指示器放在URL路径的开头,如/ en / ...或/ de / ....然后,您可以使用单个脚本来检查首选语言,并通过预先添加语言指示符来重定向请求:
// negotiate-language.php
$availableLanguages = array('en', 'de');
if (!preg_match('~^/[a-z]{2}/~', $_SERVER['REQUEST_URI'])) {
$preferedLanguage = someFunctionToDeterminThePreferedLanguage();
if (in_array($preferedLanguage, $availableLanguages)) {
header('Location: http://example.com/'.$preferedLanguage.$_SERVER['REQUEST_URI']);
} else {
// language negotiation failed!
header($_SERVER['SERVER_PROTOCOL'].' 300 Multiple Choices', true, 300);
// send a document with a list of the available language representations of REQUEST_URI
}
exit;
}
And the corresponding rules:
和相应的规则:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ negotiate-language.php [L]
RewriteRule ^([a-z]{2})/([^/]+)$ $2_$1.php [L]
Note that you need a proper someFunctionToDeterminThePreferedLanguage
function as Accept-Language header field is not a single value but a list of qualified values. So there might be more than just one value and the first value is not always the prefered value.
请注意,您需要一个正确的someFunctionToDeterminThePreferedLanguage函数,因为Accept-Language标头字段不是单个值,而是一个限定值列表。因此,可能不仅仅有一个值,第一个值并不总是首选值。
#2
2
in htaccess
在htaccess
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (en) [NC]
RewriteRule .* server.com/press_en.php [L]
RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteRule .* server.com/press_de.php [L]