且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

服务器端包含和字符编码

更新时间:2023-11-27 21:47:58

您以UTF-8的形式提供页面,这是很好的,但至少有一些页面被从实际上未保存为UTF-8的文件中拖动。 SSI只是抛出原始字节,它不尝试重新编码包含,使他们的字符集匹配它们被包括到的文件。



你需要去。



正如John所说,你可以 >通过对所有非A​​SCII字符使用字符引用避免编码问题,但这是一个巨大的痛苦。


I created this static website in which each page has the following structure:

  1. Common stuff like header, menu, etc.
  2. Page specific stuff in main content div
  3. Footer

In the website linked above all the common stuff was duplicated in each page. In order to improve the maintainability I refactored the pages to use server-side includes (SSI) so that the common parts are not duplicated. The structure of each page is now

  1. SSI for Common stuff like header, menu, etc.
  2. Page specific stuff in main content div
  3. SSI for footer

I uploaded the refactored site to this address, and as you can see it didn't quite work out. For some reason the French characters no longer display properly in the page-specific content area, though they display fine in the content included via SSIs.

The included header specifies the character set as:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

If I open one of the main content pages in a browser it tells me that the character encoding is ISO-8859-1. I've tried adding a .htaccess file to the folder with the lines

AddDefaultCharset UTF-8
AddCharset UTF-8 .shtml
AddCharset UTF-8 .html

But still those pesky French accents aren't displaying properly on the version of the site that uses SSIs.

Cheers, Don

You are serving your pages as UTF-8, which is good, but at least some of the page is being dragged in from files which are not actually saved as UTF-8. SSI just throws the raw bytes in, it doesn't attempt to recode the includes so that their charsets match the file they're being included into.

You need to go through all your html and include files in a text editor and make sure each one is saved as UTF-8.

As John mentioned, you can avoid encoding issues by using character references for all non-ASCII characters, but it's a tremendous pain.