Many
companies using WordPress to build foreign trade websiteswill create websites in multiple languages, such as English and Chinese. At the same time, some foreign trade website owners do not want their websites to be accessed by domestic users and want domestic users to be redirected to different URLs. We can use the browser language to determine the user's environment and then automatically redirect to different URLs. The specific method is as follows: Insert the following code into your website theme's header file (header.php)
<?php
// 定义变量 lc
$lc = "";
// 检查是否已经设置过 HTTP头Accept-Language信息变量
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
$lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// 这里截取语言编码前两位来判断,如果是中文,转向百度
if($lc == "zh"){
header("location: https://www.baidu.com");
exit();
}
?>Considering many are beginners and may not know how to modify code, it is recommended to install a
Code Snippets plugin. After installation, enable the plugin, edit the 'Example JavaScript snippet' snippet, and insert the above code below /* write your JavaScript code here */.

Then save to activate and enable the code. In the above code, you can add more languages, such as Brazilian, French, Portuguese, and then redirect to corresponding different URLs. The code for multiple language redirection is as follows:
<?php
$lc = "";
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
$lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if($lc == "pt"){
header("location: https://blog.naibabiji.com/");
}
else if($lc == "fr"){
header("location: https://blog.naibabiji.com/skill/language-redirection.html");
exit();
}
?>The above method references
Liaoshen's article. Below are common browser language codes.
| af | Afrikaans | sq | Albanian |
| ar-sa | Arabic (Saudi Arabia) | ar-iq | Arabic (Iraq) |
| ar-eg | Arabic (Egypt) | ar-ly | Arabic (Libya) |
| ar-dz | Arabic (Algeria) | ar-ma | Arabic (Morocco) |
| ar-tn | Arabic (Tunisia) | ar-om | Arabic (Oman) |
| ar-ye | Arabic (Yemen) | ar-sy | Arabic (Syria) |
| ar-jo | Arabic (Jordan) | ar-lb | Arabic (Lebanon) |
| ar-kw | Arabic (Kuwait) | ar-ae | Arabic (United Arab Emirates) |
| ar-bh | Arabic (Bahrain) | ar-qa | Arabic (Qatar) |
| eu | Basque | bg | Bulgarian |
| be | Belarusian | ca | Catalan |
| zh-tw | Chinese (Taiwan) | zh-cn | Chinese (Simplified, People's Republic of China) |
| zh-hk | Chinese (Hong Kong SAR) | zh-sg | Chinese (Singapore) |
| hr | Croatian | cs | Czech |
| da | Danish | nl | Dutch (Standard) |
| nl-be | Dutch (Belgium) | en | English |
| en-us | English (United States) | en-gb | English (United Kingdom) |
| en-au | English (Australia) | en-ca | English (Canada) |
| en-nz | English (New Zealand) | en-ie | English (Ireland) |
| en-za | English (South Africa) | en-jm | English (Jamaica) |
| en | English (Caribbean) | en-bz | English (Belize) |
| en-tt | English (Trinidad and Tobago) | et | Estonian |
| fo | Faroese | fa | Persian |
| fi | Finnish | fr | French (Standard) |
| fr-be | French (Belgium) | fr-ca | French (Canada) |
| fr-ch | French (Switzerland) | fr-lu | French (Luxembourg) |
| gd | Gaelic (Scottish) | ga | Irish |
| de | German (Standard) | de-ch | German (Switzerland) |
| de-at | German (Austria) | de-lu | German (Luxembourg) |
| de-li | German (Liechtenstein) | el | Greek |
| he | Hebrew | hi | Hindi |
| hu | Hungarian | is | Icelandic |
| id | Indonesian | it | Italian (Standard) |
| it-ch | Italian (Switzerland) | ja | Japanese |
| ko | Korean | ko | Korean (한국어) |
| lv | Latvian | lt | Lithuanian |
| mk | Macedonian (Former Yugoslav Republic of Macedonia) | ms | Malay |
| mt | Maltese | no | Norwegian (Bokmål) |
| no | Norwegian (Nynorsk) | pl | Polish |
| pt-br | Portuguese (Brazil) | pt | Portuguese (Portugal) |
| rm | Romansh | ro | Romanian |
| ro-mo | Romanian (Republic of Moldova) | ru | Russian |
| ru-mo | Russia (Republic of Moldova) | sz | Sami (Lappish) |
| sr | Serbian (Cyrillic) | sr | Serbian (Latin) |
| sk | Slovak | sl | Slovenian |
| sb | Sorbian | es | Spanish (Spain) |
| es-mx | Spanish (Mexico) | es-gt | Spanish (Guatemala) |
| es-cr | Spanish (Costa Rica) | es-pa | Spanish (Panama) |
| es-do | Spanish (Dominican Republic) | es-ve | Spanish (Venezuela) |
| es-co | Spanish (Colombia) | es-pe | Spanish (Peru) |
| es-ar | Spanish (Argentina) | es-ec | Spanish (Ecuador) |
| es-cl | Spanish (Chile) | es-uy | Spanish (Uruguay) |
| es-py | Spanish (Paraguay) | es-bo | Spanish (Bolivia) |
| es-sv | Spanish (El Salvador) | es-hn | Spanish (Honduras) |
| es-ni | Spanish (Nicaragua) | es-pr | Spanish (Puerto Rico) |
| sx | Sutu | sv | Swedish |
| sv-fi | Swedish (Finland) | th | Thai |
| ts | Tsonga | tn | Setswana |
| tr | Turkish | uk | Ukrainian |
| ur | Urdu | ve | Venda |
| vi | Vietnamese | xh | Xhosa |
| ji | Yiddish | zu | Zulu |
Multiple language detection is implemented using PHP - if...elseif....else statements.
if (条件)
{
if 条件成立时执行的代码;
}
elseif (条件)
{
elseif 条件成立时执行的代码;
}
else
{
条件不成立时执行的代码;
}In addition to the method above, you can also insert the following code to achieve redirection for different languages.
<?php
$lan = substr( $HTTP_ACCEPT_LANGUAGE,0,5);
if ($lan == "zh-cn")
print("<meta http-equiv='refresh' content = '0;URL = gb/index.htm'>");
else
print("<meta http-equiv='refresh' content = '0;URL = eng/index.htm'>");If you are using a translation plugin to create a multilingual website, the plugin settings usually include an option to enable redirection directly. Besides modifying code, you can also use domain name resolution or direct IP address targeting for redirection; some advanced DNS service providers support this feature.
Other related articles previously written:
Comments are closed
The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.