3

I'm using:

ForceType text/html;charset=utf-8

in my .htaccess file, but it's causing all externally linked CSS to stop rendering on their respective pages.

So something like this:

<link rel="stylesheet" type="text/css" href="somestyles.css" media="all" />

no longer works on the page using it.

I've also been trying combinations of:

AddCharset utf-8 .html
AddCharset utf-8 .htm
AddCharset utf-8 .css
AddCharset utf-8 .js
ForceType text/html;charset=utf-8
ForceType text/css;charset=utf-8

but no luck so far. Does anyone know what's wrong?

chimerical
  • 5,883
  • 8
  • 31
  • 37

2 Answers2

7

This is all you need to get UTF-8 for both CSS and Javascript files:

AddDefaultCharset utf-8
AddCharset utf-8 .css .js
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
  • Don't assume mod_mime is enabled. (I did) Debian doesn't enable it by default. Wrap these in a just to be sure. – txyoji Jul 24 '18 at 17:47
5

This works for me on my web host (in my root .htaccess):

#for html, php
AddDefaultCharset UTF-8
#for css, js
AddType 'text/css; charset=UTF-8' css
AddType 'application/x-javascript; charset=UTF-8' js

I'm not sure, but I think the ForceType may be telling the browser that the css file is html, not css.

UPDATE:

First of all, application/x-javascript should not be used anymore; it should be application/javascript (x- meant experimental). But it's even simpler than that, at least for Apache 2.2.22

#for html, php
AddDefaultCharset UTF-8
#for css, js, etc.
AddCharset UTF-8 .js .css

Using AddType also sets the MIME type, which Apache is likely to take care of correctly, while my original answer set it incorrectly for javascript (as of today). So let Apache do its job on MIME types and use the shorter and simpler single-line AddCharset for all additional types.

Slashback
  • 541
  • 5
  • 9