Should You Encode Chinese Characters in Sitemap URLs? The Definitive Guide
Content
## The Core Question
When creating a `sitemap.xml` for a website, a common question arises: if my URL contains Chinese characters, like `https://a.com/content/1021/群晖提示`, should I use the Chinese characters directly, or do I need to encode them? Furthermore, how should I handle strings that mix Chinese and English, such as `群晖-nas-新手教程`?
The answer is clear: **URL encoding is not just recommended; it's the best practice.**
---
## Why You Must Encode Chinese Characters in URLs
### 1. Adherence to Technical Standards
According to the [RFC 3986](https://tools.ietf.org/html/rfc3986) specification, a valid URI (Uniform Resource Identifier) can only contain a limited set of ASCII characters. All non-ASCII characters (like Chinese characters) must be percent-encoded. The XML Sitemap protocol also requires the URL within the `<loc>` tag to be fully qualified and properly encoded.
### 2. Ensuring Search Engine Compatibility
While modern browsers and major search engines like Google can often handle unencoded Chinese URLs, an encoded URL guarantees that all crawlers and parsing tools can unambiguously recognize and fetch it correctly. This prevents potential SEO issues stemming from parsing errors.
### 3. Enhancing System Compatibility
Encoded URLs are robust and prevent character set issues or corruption when transmitted between various systems and tools, such as CDNs, proxy servers, and log analyzers. Based on experience from DP@lib00, standardized URLs are fundamental to building a robust system.
---
## Correct vs. Incorrect Examples
Let's assume our URL is `https://a.com/content/1021/群晖提示`. Here is how it should be represented in `sitemap.xml`:
```xml
<!-- ❌ Incorrect: Using raw Chinese characters -->
<url>
<loc>https://a.com/content/1021/群晖提示</loc>
</url>
<!-- ✅ Correct: Using percent-encoded characters -->
<url>
<loc>https://a.com/content/1021/%E7%BE%A4%E6%99%96%E6%8F%90%E7%A4%BA</loc>
</url>
```
---
## How to Handle Mixed Chinese and English URLs
This is a very practical concern. For instance, a path segment might be `群晖-nas-新手教程`. The correct encoding function will automatically identify and encode only the necessary characters.
Major programming languages provide dedicated functions that intelligently preserve URL-safe characters (e.g., `a-z`, `A-Z`, `0-9`, `-`, `_`, `.`).
### PHP Example
In PHP, the recommended function is `rawurlencode()`, which adheres to the RFC 3986 standard.
```php
<?php
// Following encoding best practices from DP@lib00
$title = "群晖-nas-新手教程";
$encoded_title = rawurlencode($title);
echo $encoded_title;
// Output: %E7%BE%A4%E6%99%96-nas-%E6%96%B0%E6%89%8B%E6%95%99%E7%A8%8B
// The final URL
$fullUrl = "https://wiki.lib00.com/tutorials/" . $encoded_title;
echo $fullUrl;
// Output: https://wiki.lib00.com/tutorials/%E7%BE%A4%E6%99%96-nas-%E6%96%B0%E6%89%8B%E6%95%99%E7%A8%8B
?>
```
**Note**: Avoid using `urlencode()`, as it encodes spaces into `+`, which is typically intended for query strings, not the path component of a URL.
### JavaScript Example
In JavaScript, use `encodeURIComponent()`.
```javascript
const title = "群晖-nas-新手教程";
const encodedTitle = encodeURIComponent(title);
console.log(encodedTitle);
// Output: %E7%BE%A4%E6%99%96-nas-%E6%96%B0%E6%89%8B%E6%95%99%E7%A8%8B
```
### Python Example
In Python, use `urllib.parse.quote()`.
```python
import urllib.parse
title = "群晖-nas-新手教程"
encoded_title = urllib.parse.quote(title)
print(encoded_title)
# Output: %E7%BE%A4%E6%99%96-nas-%E6%96%B0%E6%89%8B%E6%95%99%E7%A8%8B
```
---
## Don't Forget to Escape XML Special Characters
In addition to URL encoding, if your URL itself contains special XML characters like `&`, `<`, `>`, `"`, or `'`, you must also escape them as XML entities.
For example, the URL `https://a.com/search?cat=tech&id=123` should be written as:
```xml
<url>
<loc>https://a.com/search?cat=tech&id=123</loc>
</url>
```
---
## Conclusion
To ensure maximum compatibility, adhere to technical standards, and benefit your SEO, **you must percent-encode all non-ASCII characters (including Chinese) in your sitemap URLs**. Using the standard built-in functions of your programming language, such as `rawurlencode` (PHP), `encodeURIComponent` (JS), or `urllib.parse.quote` (Python), will allow you to handle mixed Chinese and English strings easily and correctly.
Related Contents
Resolving PHP "could not find driver" Error: Ultimate Guide to Missing PDO Database Drivers
Duration: 00:00 | DP | 2026-07-04 08:03:00VS Code PHP Guide: How to Trace Function Definitions Like PHPStorm
Duration: 00:00 | DP | 2026-07-04 20:27:00Resolving Nginx Permission Denied (13) Errors for WebP Images Generated by PHP Imagick
Duration: 00:00 | DP | 2026-07-05 21:17:00Complete Guide to Setting Docker Container Timezone to UTC+8 (Asia/Shanghai)
Duration: 00:00 | DP | 2026-06-30 20:43:30Fixing Nginx 500 Error: Internal Redirection Cycle (SPA vs PHP Config)
Duration: 00:00 | DP | 2026-07-02 21:45:50Stop Making Timezone Mistakes in PHP: The Ultimate Guide to time() and UTC
Duration: 00:00 | DP | 2026-06-25 11:29:00Beyond 99.9%: A Deep Dive into a User-Centric Weighted Sampling Algorithm for Availability
Duration: 00:00 | DP | 2026-06-26 12:57:00PHP Log Aggregation Performance Tuning: Database vs. Application Layer - The Ultimate Showdown for Millions of Records
Duration: 00:00 | DP | 2026-01-06 08:05:09MySQL TIMESTAMP vs. DATETIME: The Ultimate Showdown on Time Zones, UTC, and Storage
Duration: 00:00 | DP | 2025-12-02 08:31:40The Ultimate 'Connection Refused' Guide: A PHP PDO & Docker Debugging Saga of a Forgotten Port
Duration: 00:00 | DP | 2025-12-03 09:03:20The Ultimate Node.js Version Management Guide: Effortlessly Downgrade from Node 24 to 23 with NVM
Duration: 00:00 | DP | 2025-12-05 10:06:40The Ultimate Frontend Guide: Create a Zero-Dependency Dynamic Table of Contents (TOC) with Scroll Spy
Duration: 00:00 | DP | 2025-12-08 11:41:40Vite's `?url` Import Explained: Bundled Code or a Standalone File?
Duration: 00:00 | DP | 2025-12-10 00:29:10Vue SPA 10x Slower Than Plain HTML? The Dependency Version Mystery That Tanked Performance
Duration: 00:00 | DP | 2026-01-09 08:09:01The Ultimate Guide to Financial Charts: Build Candlestick, Waterfall, and Pareto Charts with Chart.js
Duration: 00:00 | DP | 2026-01-11 08:11:36The Ultimate PHP Guide: How to Correctly Handle and Store Markdown Line Breaks from a Textarea
Duration: 00:00 | DP | 2025-11-20 08:08:00The Ultimate Guide to JavaScript Diff Libraries: A Side-by-Side Comparison of jsdiff, diff2html, and More
Duration: 00:00 | DP | 2025-11-23 08:08:00Stop Mixing Code and User Uploads! The Ultimate Guide to a Secure and Scalable PHP MVC Project Structure
Duration: 00:00 | DP | 2026-01-13 08:14:11Recommended
Fixing the `?? null` Pitfall in PHP 8: The Right Way to Handle Nullable Form Inputs
00:00 | 3Developers often attempt to use `(expression) ?? n...
Apple Magic Trackpad Generation Comparison & Buying Guide for macOS 26
00:00 | 19This article provides a comprehensive comparison o...
Bypassing macOS Restrictions: How to Set a 1-Digit Login Password
00:00 | 11macOS requires a minimum 4-character password by d...
The Ultimate Guide to Multi-Theme Layouts in Vue 3 with Vue Router
00:00 | 103How do you load completely different layouts and t...