Home Experience & Tips Sharing Standard Post

Standard Post

How to automatically redirect URLs in WordPress based on browser language

Many companies using WordPress to build foreign trade websites create multilingual sites, such as English and Chinese. At the same time, some foreign trade site 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: On your website...

Published on October 13, 2020 About 8 minutes read
WordPress根据浏览器语言自动跳转网址的方法

Manycompanies using WordPress to build foreign trade websitescompanies create multilingual sites, such as English and Chinese. At the same time, some foreign trade site 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 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 aCode 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 the code.

In the code above, you can add more languages, such as Brazilian, French, Portuguese, and then redirect to different URLs accordingly. The multi-language redirect code 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 referencesLiaoshen's article. Below are common browser language codes.

afAfrikaanssqAlbanian
ar-saArabic (Saudi Arabia)ar-iqArabic (Iraq)
ar-egArabic (Egypt)ar-lyArabic (Libya)
ar-dzArabic (Algeria)ar-maArabic (Morocco)
ar-tnArabic (Tunisia)ar-omArabic (Oman)
ar-yeArabic (Yemen)ar-syArabic (Syria)
ar-joArabic (Jordan)ar-lbArabic (Lebanon)
ar-kwArabic (Kuwait)ar-aeArabic (United Arab Emirates)
ar-bhArabic (Bahrain)ar-qaArabic (Qatar)
euBasquebgBulgarian
beBelarusiancaCatalan
zh-twChinese (Taiwan)zh-cnChinese (Simplified, People's Republic of China)
zh-hkChinese (Hong Kong SAR)zh-sgChinese (Singapore)
hrCroatiancsCzech
daDanishnlDutch (Standard)
nl-beDutch (Belgium)enEnglish
en-usEnglish (United States)en-gbEnglish (United Kingdom)
en-auEnglish (Australia)en-caEnglish (Canada)
en-nzEnglish (New Zealand)en-ieEnglish (Ireland)
en-zaEnglish (South Africa)en-jmEnglish (Jamaica)
enEnglish (Caribbean)en-bzEnglish (Belize)
en-ttEnglish (Trinidad and Tobago)etEstonian
foFaroesefaPersian
fiFinnishfrFrench (Standard)
fr-beFrench (Belgium)fr-caFrench (Canada)
fr-chFrench (Switzerland)fr-luFrench (Luxembourg)
gdGaelic (Scottish)gaIrish
deGerman (Standard)de-chGerman (Switzerland)
de-atGerman (Austria)de-luGerman (Luxembourg)
de-liGerman (Liechtenstein)elGreek
heHebrewhiHindi
huHungarianisIcelandic
idIndonesianitItalian (Standard)
it-chItalian (Switzerland)jaJapanese
koKoreankoKorean (한국어)
lvLatvianltLithuanian
mkMacedonian (Former Yugoslav Republic of Macedonia)msMalay
mtMaltesenoNorwegian (Bokmål)
noNorwegian (Nynorsk)plPolish
pt-brPortuguese (Brazil)ptPortuguese (Portugal)
rmRomanshroRomanian
ro-moRomanian (Republic of Moldova)ruRussian
ru-moRussia (Republic of Moldova)szSami (Lappish)
srSerbian (Cyrillic)srSerbian (Latin)
skSlovakslSlovenian
sbSorbianesSpanish (Spain)
es-mxSpanish (Mexico)es-gtSpanish (Guatemala)
es-crSpanish (Costa Rica)es-paSpanish (Panama)
es-doSpanish (Dominican Republic)es-veSpanish (Venezuela)
es-coSpanish (Colombia)es-peSpanish (Peru)
es-arSpanish (Argentina)es-ecSpanish (Ecuador)
es-clSpanish (Chile)es-uySpanish (Uruguay)
es-pySpanish (Paraguay)es-boSpanish (Bolivia)
es-svSpanish (El Salvador)es-hnSpanish (Honduras)
es-niSpanish (Nicaragua)es-prSpanish (Puerto Rico)
sxSutusvSwedish
sv-fiSwedish (Finland)thThai
tsTsongatnSetswana
trTurkishukUkrainian
urUrduveVenda
viVietnamesexhXhosa
jiYiddishzuZulu

Multiple language detection uses PHP's if...elseif...else statement.

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 some translation plugins to create a multilingual website, the plugin settings usually have a redirect feature that can be enabled directly.

In addition to modifying code, you can also use domain resolution to directly redirect based on IP address location. Some advanced DNS service providers support this feature.

Other related articles previously written:

Rate this article post
Previous A Website Building Tutorial for Foreign Trade VPS Scala Hosting – Simpler than Shared Hosting Continue reading content around the same timeline. Next 4 WordPress Plugins That Require a Password to Access the Website_Password to View Content View the next related tutorial or experience.

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: