Problem Description
If your website serves both English-speaking and Chinese-speaking audiences, you may want to separate the English and Chinese content into distinct sections. This improves user experience, SEO performance, and content management. A common approach is to use subdirectories (e.g., /en/ and /zh/) or subdomains (e.g., en.example.com and zh.example.com) on a Linux server running Nginx or Apache.
This article walks you through configuring your Linux web server to properly serve separated English and Chinese versions of your site, including correct language headers, directory structure, and redirection rules.
Solution Steps
Step 1: Organize Your Directory Structure
Create separate directories for each language version under your web root:
/var/www/html/en/
/var/www/html/zh/
Place the English content files (e.g., index.html) inside /en/ and Chinese content files inside /zh/.
Step 2: Configure Nginx for Language-Based Separation
Edit your Nginx server block configuration file (commonly located at /etc/nginx/sites-available/your-site.conf):
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
# Default redirect to English version
location = / {
return 302 /en/;
}
# English version
location /en/ {
try_files $uri $uri/ /en/index.html;
add_header Content-Language en;
}
# Chinese version
location /zh/ {
try_files $uri $uri/ /zh/index.html;
add_header Content-Language zh;
}
}
After saving the file, test and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
Step 3: Configure Apache for Language-Based Separation
If you are using Apache, edit your virtual host configuration (commonly at /etc/apache2/sites-available/your-site.conf):
ServerName example.com
DocumentRoot /var/www/html
# Default redirect to English
RedirectMatch ^/$ /en/
Header set Content-Language "en"
Header set Content-Language "zh"
Enable the required modules and restart Apache:
sudo a2enmod headers
sudo systemctl restart apache2
Step 4: Add hreflang Tags for SEO
Include hreflang tags in the section of each page to help search engines understand the language relationship:
Step 5: Implement Automatic Language Redirection (Optional)
You can use the browser's Accept-Language header to automatically redirect users to the appropriate language version. Add the following to your Nginx configuration:
set $lang_prefix /en/;
if ($http_accept_language ~* "zh") {
set $lang_prefix /zh/;
}
location = / {
return 302 $lang_prefix;
}
Additional Tips
- Character Encoding: Ensure your HTML files declare
in thesection to properly display Chinese characters. - Font Support: Verify that your web fonts support Chinese glyphs. Common choices include Noto Sans SC, PingFang SC, and Microsoft YaHei.
- Content Management: Consider using a CMS (such as WordPress with WPML, or a static site generator with i18n support) to manage translations more efficiently rather than maintaining duplicate static files.
- Language Switcher: Add a visible language switcher (e.g., EN | 中文) in your website's navigation bar so users can manually switch between versions.
- Testing: Use tools like
curl -H "Accept-Language: zh" https://example.comto verify that automatic redirection works correctly. - Subdomain Alternative: If you prefer subdomains (
en.example.comandzh.example.com), configure separate server blocks or virtual hosts for each subdomain and point them to the respective content directories.