- 从linux服务器nginx上把一个网站迁移到windows的IIS上
数据什么的都么有问题,配置好rewrite以后,访问网站,发现样式变动了,网站上方空出了一块
我用chrome浏览器的审查元素一看,发现head里的内容全到Body里了,而且body的最开始出多出了一块带引号的空白!
但是如果右键查看源代码的话,代码是正确的,没有问题!
正常状态

错误状态
求大牛解救
问题原因:
网站采用了UTF-8无BOM编码,但是在使用include或者require包含文件的时候,包含了一个UTF-8有BOM的文件,就产生上述现象。
解决方法:
对网站所有文件进行去BOM操作
clearBOM.php(放到根目录下执行,执行前先备份网站,以备不测)
01 | <?php |
02 | $basedir = str_replace('/clearBOM.php','',str_replace('\\','/',dirname(__FILE__))); |
03 | $auto = 1; |
04 | checkdir($basedir); |
05 | function checkdir($basedir){ |
06 | if ($dh = opendir($basedir)) { |
07 | while (($file = readdir($dh)) !== false) { |
08 | if ($file != '.' && $file != '..'){ |
09 | if (!is_dir($basedir.'/'.$file)) { |
10 | $filename = $basedir.'/'.$file; |
11 | echo 'filename:'.$basedir.'/'.$file.checkBOM($filename).'<br>'; |
12 | } else { |
13 | $dirname = $basedir.'/'.$file; |
14 | checkdir($dirname); |
15 | } |
16 | } |
17 | } |
18 | closedir($dh); |
19 | } |
20 | } |
21 |
22 | function checkBOM ($filename) { |
23 | global $auto; |
24 | $contents = file_get_contents($filename); |
25 | $charset[1] = substr($contents, 0, 1); |
26 | $charset[2] = substr($contents, 1, 1); |
27 | $charset[3] = substr($contents, 2, 1); |
28 | if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) { |
29 | if ($auto == 1) { |
30 | $rest = substr($contents, 3); |
31 | rewrite ($filename, $rest); |
32 | return '<font color=red>BOM found,automatically removed.</font>'; |
33 | } else { |
34 | return '<font color=red>BOM found.</font>'; |
35 | } |
36 | } else { |
37 | return 'BOM Not Found.'; |
38 | } |
39 | } |
40 |
41 | function rewrite ($filename, $data) { |
42 | $filenum = fopen($filename, 'w'); |
43 | flock($filenum, LOCK_EX); |
44 | fwrite($filenum, $data); |
45 | fclose($filenum); |
46 | } |
47 | ?> |
本文探讨了在将网站从Linux服务器的nginx迁移到Windows的IIS过程中,遇到样式变动的问题。问题源于文件编码不一致,具体为网站采用UTF-8无BOM编码,但在使用include或require包含文件时,引入了一个UTF-8有BOM的文件。通过执行clearBOM.php脚本,对所有网站文件进行去BOM操作,解决了样式问题。
390





