0

I have an old web application (not maintained anymore) of which a small part is still needed; this is displayed in iframes.

Unfortunately, the old application uses ISO-8859-1 encoding (which it doesn't declare; it simply sends Content-Type: text/html), while the new one (of course) uses UTF-8. Thus, since the encoding of the iframe contents is not specified, UTF-8 is expected, and thus several characters look wrong.

The VirtualHost of the old application does some rewriting to a JBoss application:

RewriteRule ^/(.*) http://localhost:9080/$1 [L,P]

I'm able to edit this VirtualHost (while nobody will touch the application itself anymore); how would I add the encoding information?

E.g., for all contents with a textual content type which lack encoding information (naked Content-Type: text/html), append ; charset: ISO-8859-1. Any simple code snippet? Thank you!

1

The HTTP header you need to add is Content-Type: text/html; charset: ISO-8859-1.


You can add FilesMatch and other directives within the Apache <VirtualHost> declaration:

<VirtualHost>
  # other directives here such as DocumentRoot
  <FilesMatch "\.(htm|html|php)$">
    AddDefaultCharset ISO-8859-1
    Header always set Content-Type "text/html; charset: ISO-8859-1"
  </FilesMatch>
  <FilesMatch "\.(css)$">
    AddDefaultCharset ISO-8859-1
    Header always set Content-Type "text/css; charset: ISO-8859-1"
  </FilesMatch>
  <FilesMatch "\.(js)$">
    AddDefaultCharset ISO-8859-1
    Header always set Content-Type "text/javascript; charset: ISO-8859-1"
  </FilesMatch>
  RewriteRule ^/(.*) http://localhost:9080/$1 [L,P]
</VirtualHost>
  • As I said, I won't touch the old application, so neither the PHP nor the HTML hint applies. And I don't serve out of a directory structure, so .htaccess doesn't apply, either. I need to add the charset information for textual content (e.g. not for images), and of course only unless already given. – Tobias May 28 '15 at 14:54
  • I've edited the solution to explain that the PHP/HTML snippets are a secondary fallback method, and that the Header can be set from within the VirtualHost declaration too, not just from within .htaccess files. – richhallstoke May 28 '15 at 15:05
  • Yes, but not everything is HTML text - there are images as well. So I need to affect text/html only. (There might even be pages which have the charset defined.) Wouldn't this Header always set part overwrite e.g. image/png types? – Tobias May 28 '15 at 15:29
  • I've modified the solution following your comment to show how directives can be nested inside the VirtualHost declaration, without seeing your current VirtualHost code it is very hard to try and replicate but hopefully the snippet above is close enough for you to customise how you need. – richhallstoke May 28 '15 at 15:51
  • Thanks für the hint to AddDefaultCharset; it seems to do everything I need (no further filtering necessary). – Tobias May 29 '15 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.