CentOS 下 Apache 默認配置 AddDefaultCharset UTF-8,導致無 meta charset 聲明的 GBK/GB2312 頁面顯示亂碼。
vi /etc/httpd/conf/httpd.confAddDefaultCharset:AddDefaultCharset UTF-8AddDefaultCharset OffAddDefaultCharset GB2312systemctl restart httpd 或 service httpd restart| 方案 | 優點 | 缺點 | 適用場景 |
|---|---|---|---|
| Off | 完全由頁面控制,兼容 UTF-8/GBK 混合站點 | 頁面必須正確設置 meta charset | 現代項目、多編碼共存 |
| GB2312 | 舊項目零改動即可正常顯示 | UTF-8 頁面會被強制誤判為 GBK 導致亂碼 | 純 GBK 老舊系統、無法修改頁面代碼 |
<?php
// PHP 輸出頭(最優先,覆蓋 Apache)
header('Content-Type: text/html; charset=gb2312'); // 或 utf-8
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
...
curl -I https://yourdomain.com/gbk-page.php
# 檢查響應頭 Content-Type 是否包含 charset=gb2312
# 或無 charset (AddDefaultCharset Off 時)