🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

How to automatically redirect URLs in WordPress based on browser language

Manycompanies 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 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 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 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 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:

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step.

「Naibabiji WordPress Website Building Coaching Service」—From choosing a domain and buying hosting, to installing a Theme and publishing content, I「ll coach you through every step, helping you avoid detours and reach your goal directly.

👉 Learn about the Website Building Coaching Service
🔒

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.

×
二维码

Scan QR Code to Follow

AI Website Building Assistant

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