让Apache正确处理不同编码的文件避免中文乱码

安装了apache2.4.39以后,默认编码是UTF-8,不管你文件是什么编码,统统按这个来解析,因此 GB2312编码文件内的中文将显示为乱码。

php 复制代码
<!doctype html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=GB2312"> 
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="renderer" content="webkit">
	<meta name="force-rendering" content="webkit">
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />	
	<meta http-equiv="Content-Language" content="zh-cn">
    </head>
    <body>	
	<span>中文测试</span> <hr/>
<?php
    header('Content-type:text/html;charset=GB2312');
    // $input_filed = '<input type="text" name="username" value="'.$_POST['username'].'">';
    // echo $input_filed;
    $input_name = '<input type = "text" name = username value = "'.$_POST['username'].'">';
    echo '请输入姓名:' . $input_name;
    echo '<hr/>';
	echo "中文测试";
    // //Example #1 $_POST 范例
    // echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
?>

</body>
</html>

解决方案:

打开apache配置文件 \Apachexxx\conf\httpd.conf ,查找AddDefaultCharset,查看设置的是啥,改为

AddDefaultCharset off

如果配置文件中没有AddDefaultCharset的话就加上一句

AddDefaultCharset off

改完了记得重启Apache

这样,Apache就能按照文件中设定的编码来正确解析文件了,前提是你的文件存储格式必须与设定编码一致。

如果不设置 AddDefaultCharset ,那么新安装完Apache后,默认都是用 UTF-8 编码来解析页面的。

相关推荐
Debug_TheWorld1 天前
Apache Flink 深度解析:流处理引擎的核心原理与生产实践指南
大数据·flink·apache
Apache IoTDB2 天前
Apache IoTDB V2.0.2/V1.3.4 发布|新增表模型权限管理、UDF、嵌套查询功能
apache·iotdb
ashane13142 天前
Apache PDFBox
apache
三次握手四次挥手2 天前
基于Python+Pytest实现自动化测试(全栈实战指南)
开发语言·python·自动化·k8s·apache·pytest·代码规范
自由如风7093 天前
Apache Atlas构建安装(Linux)
linux·运维·apache
真就死难3 天前
ArcGIS、ArcMap查看.shp文件时属性表中文乱码
arcgis·中文乱码
techdashen3 天前
性能比拼: Nginx vs Apache
运维·nginx·apache
w23617346013 天前
解析三大中间件:Nginx、Apache与Tomcat
nginx·中间件·tomcat·apache
Minyy114 天前
使用注解方式整合ssm时,启动tomcat扫描不到resource下面的xxxmapper.xml问题,解决方法
java·spring boot·spring·apache·mybatis
斯普信专业组8 天前
二进制和docker两种方式部署Apache pulsar(standalone)
docker·容器·apache