Set charset in ErrorDocument messages

I have a .htaccess file that looks like this:

 AddDefaultCharset utf-8 AddCharset utf-8 .html Order Allow,Deny ErrorDocument 403 "Error 403 - Esta ubicación no es pública" 

The file itself is encoded as UTF-8. However, Apache insists on declaring ISO-8859-1, and the error message is garbled:

 HTTP/1.1 403 Forbidden Date: Fri, 29 Nov 2013 10:06:25 GMT Server: Apache/2.4.6 (Win32) OpenSSL/1.0.1e PHP/5.5.6 Content-Length: 42 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html; charset=iso-8859-1 

If I manually change the encoding to UTF-8, the text looks correct in my browser.

The site received all permissions:

 <VirtualHost *:80> ServerName tmp DocumentRoot "D:/tmp" <Directory "D:/tmp"> AllowOverride All Require all granted </Directory> </VirtualHost> 

... and in Apache logs nothing is significant.

What bit am I missing?

+7
apache utf-8 .htaccess
source share
1 answer

Awesome question !! I have to say. I had to dig all my resources and read many manuals to find the reason for this behavior.

This is apparently a well-known behavior, but not well covered in the official manuals. Finally, I found one link in this Apache manual :

suppress encoding errors

Available in versions after 2.0.54

When Apache issues a redirect in response to a client request, the response includes some actual text to display if the client cannot (or does not) automatically follow the redirect. apache usually designates this text according to the character set that it uses ISO-8859-1.

However, if the redirect refers to a page that uses a different character set, some broken versions of the browser will try to use the character set from the redirect text, rather than the actual page. This may cause the Greek language to display incorrectly.

Setting this environment variable causes Apache to skip the character for the redirect text , and these broken browsers will then correctly use the destination page.

And this is exactly the behavior you see that charset=iso-8859-1 is included in the headers.


How to fix:

Enter the .htaccess code as follows:

 # set desired env variable to suppress iso-8859-1 charset SetEnvIf Host ^ suppress-error-charset # set desired 403 message with desired charset ErrorDocument 403 "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body>Error 403 - Esta ubicación no es pública</body></html>" 

Note that SetEnvIf Host ^ is a condition that will always be true, so suppress-error-charset will always be set. I tested only these 2 lines in my .htaccess and received the correct error message displayed in my browser.

+9
source share

All Articles