Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set character encoding to UTF-8 for default.html?

I spent the last few hours getting my website to validate HTML 4.01 Strict and I actually have succeeded in that, but there is still one warning which I can't get rid of. The warning is:

Character Encoding mismatch!

The character encoding specified in the HTTP header (iso-8859-1) is different from the value in the element (utf-8). I will use the value from the HTTP header (iso-8859-1) for this validation.

The page in question is www.dubiousarray.net/default.html. As you can see from the page source I have the following meta element:

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

and I have made sure that the default.html file is saved with UTF-8 encoding. The strange thing is all the other pages in the site validate without this warning and they have the same meta tag and were saved in exactly the same way. I am pretty sure it is something to do with the server configuration. The .htaccess file looks like this at the moment:

# Use PHP 5 as default
AddHandler application/x-httpd-php5 .php
AddDefaultCharset UTF-8

But I have tried all the fixes shown on this page and none of them worked. How can I go about getting rid of this warning?

In Firefox, if you right click on the page and select 'View Page Info', default.html shows as ISO-8859-1, while all the other pages show UTF-8.

All the html file have been created and saved in the exact same way (character encoding set to UTF-8 without BOM), but default.html is the only one which isn't displaying as UTF-8. So I assume the server is doing something special to the default.html file though I am not sure what as there is not sign of it in the .htaccess file.

like image 650
Jacob de Lacey Avatar asked May 25 '09 02:05

Jacob de Lacey


People also ask

How do I set HTML to UTF-8?

The character encoding should be specified for every HTML page, either by using the charset parameter on the Content-Type HTTP response header (e.g.: Content-Type: text/html; charset=utf-8 ) and/or using the charset meta tag in the file.

How do I specify character encoding in HTML?

DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> ... It doesn't matter which you use, but it's easier to type the first one. It also doesn't matter whether you type UTF-8 or utf-8 . You should always use the UTF-8 character encoding.

What is default HTML encoding?

The default character encoding for HTML5 is UTF-8.


1 Answers

You need to replace the HTTP-level header.

This should work:

<?php
    header('Content-type: text/html; charset=utf-8');
?>

Note that the above must be the first thing in your file. No exceptions. See header.

For general information on how to change the character set header in different web stacks, see Setting the HTTP charset parameter.

like image 153
Larry K Avatar answered Sep 17 '22 20:09

Larry K