When serving JSON files through Apache, many developers encounter character encoding issues, especially when dealing with non-ASCII characters like French accents. While setting Content-Type: application/json
is common, omitting the charset can lead to misinterpretation of special characters in client-side JavaScript.
The AddDefaultCharset utf-8
directive in httpd.conf is a global setting that applies to all content types. However, JSON files often need explicit charset declaration because:
// Without proper charset, French characters may appear broken
{
"message": "Déjà vu", // Might render as "Déjà vu"
"status": "success"
}
Create or modify your .htaccess
file with these rules:
<FilesMatch "\.json$">
ForceType application/json; charset=utf-8
Header set Content-Type "application/json; charset=utf-8"
</FilesMatch>
Alternatively, in your virtual host configuration:
<Location "/data/">
AddType application/json .json
AddCharset utf-8 .json
Header set Content-Type "application/json; charset=utf-8"
</Location>
After implementation, verify using curl:
curl -I http://yourserver.com/data/example.json
Should return:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
For APIs serving dynamic JSON, consider adding this to your PHP or other server-side code:
<?php
header('Content-Type: application/json; charset=utf-8');
?>
For Node.js applications using Apache as reverse proxy:
# In Apache config
ProxyPass "/api/" "http://localhost:3000/"
ProxyPassReverse "/api/" "http://localhost:3000/"
Header always set Content-Type "application/json; charset=utf-8"
When serving JSON files through Apache, many developers encounter charset-related issues, especially when dealing with non-ASCII characters (like French accents, German umlauts, or Chinese characters). While setting Content-Type: application/json
is common, the missing charset declaration can cause parsing issues in client-side JavaScript.
The AddDefaultCharset utf-8
directive in httpd.conf doesn't guarantee charset specification for JSON files because:
- Default charset might be overridden by other configuration
- Some MIME type handlers don't respect default charset settings
- The directive primarily affects text/html content
1. Using AddType with Charset
The most effective method is to explicitly define both content type and charset:
# In httpd.conf or .htaccess
AddType application/json;charset=UTF-8 .json
2. Using Header Directive
For more control, set headers conditionally:
<FilesMatch "\.json$">
Header set Content-Type "application/json; charset=UTF-8"
</FilesMatch>
3. Using ForceType with Charset
When files don't have .json extension:
<Location "/api/data">
ForceType application/json;charset=UTF-8
</Location>
After making changes, verify the headers using curl:
curl -I http://yourserver.com/data.json
Should return:
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
...
For JSONP responses (with callback parameter):
<FilesMatch "\.jsonp?$">
Header set Content-Type "application/javascript; charset=UTF-8"
</FilesMatch>
While these solutions work, remember:
- Header modifications add minor processing overhead
- For high-traffic APIs, consider setting headers at application level
- Cache headers should be properly configured for static JSON files