Last updated on July 1st, 2024 at 06:15 am
By default, Apache uses iso-8859-1 character encoding that represents the first 256 unicode characters. If your website needs to display international characters, then you need to change Apache character encoding to UTF-8. Otherwise, these characters will appear as unidentifiable symbols. In this article, we will look at how to set default charset to UTF-8 encoding in Apache using .htaccess.
What is Charset in Apache Server
Character set is a set of characters used by web server, or any program for that matter, to render data to users as well as exchange it with client web browsers. Once the default charset is set, Apache will specify the charset in response headers so that web browsers can easily decode the information. Otherwise, they will need to guess it which may cause errors. Please note, Apache does not encode the information sent to client browsers. Here is an example of response header set by Apache server.
Content-Type: text/html; charset=utf8
This information is only to inform the web browser about encoding. Apache, or any server for that matter, does not check or convert any data.
Why Set Default Charset to UTF-8
Some character sets such as ASCII support only a small set of symbols, whereas other character sets such as UTF-8 support a vast range of symbols. If your website or app contains non-English characters then ASCII or ISO-8859-1 will not be sufficient. In such cases, it is advisable to set default charset to UTF-8.
Also, since Apache uses ISO-8859-1 encoding by default, it will add the following header in each response.
Content-Type: text/html; charset=iso-8859-1
If this does not match the encoding of actual data present in server response, the web browser will simply display the information based on the response header it receives from Apache. For example, if the default character encoding on your Apache server is ISO-8859-1 and it sends certain non-English characters in response then web browsers will wrongly render them using ISO-8859-1 encoding, instead of using UTF-8 encoding.
How To Set Default Charset to UTF-8 Encoding in Apache
Here are the steps to set default charset to UTF-8 encoding in Apache using htaccess.
1. Open Apache Config or htaccess file
We will need to use AddDefaultCharset for our purpose. You can add it to either Apache config file, or .htaccess file. If you have access to Apache config file then first take its backup.
$ sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd-backup.conf
Open the config file in a text editor.
$ sudo vi /etc/apache2/httpd.conf
If you do not have access to Apache config file, then open .htaccess file. In this case, before proceeding, please enable mod_rewrite (.htaccess) in your Apache web server.
Open .htaccess file, typically located at /var/www/html/.htaccess
$ sudo vi /var/www/html/.htaccess
2. Set Default Encoding to UTF-8
We will need to use AddDefaultCharset directive in Apache server to set default encoding. Add the following to Apache config file or .htaccess file.
AddDefaultCharset UTF-8
This will be applicable for all files served by Apache web server.
For Specific File Types
If you want to apply UTF-8 encoding only for specific file types, use the filesMatch directive.
<filesMatch "\.(html|css|js|txt|php)$">
AddDefaultCharset UTF-8
DefaultLanguage en-US
</filesMatch>
Alternatively, you can add the following line to do the same.
AddCharset utf-8 .html .css .php .txt .js
For Specific Directory
If you do not want to change character encoding for your entire website, but only for a specific directory and its contents, then create .htaccess file in it and add AddDefaultCharset to it.
3. Restart Apache Web Server
Restart Apache web server to apply changes.
$ sudo service apache2 restart
That’s it. The default character encoding on your website will be changed to UTF8.
Conclusion
In this article, we have learnt how to set default character encoding in Apache server. You can add the AddDefaultChartset directive to either Apache config file or .htaccess file, depending on your requirement. Please note, if you add AddDefaultCharset to Apache config file, it will apply the changes to all websites and virtual hosts that are running on the server. If you want to limit the impact of this change, then create .htaccess file in the directory where you want to set charset encoding and then add this directive. This will apply the change only to the web root folder or directory where .htaccess file is located.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!
Also Read :
How to Enable mod_rewrite in XAMPP, WAMP
How to Change Default Index Page
How to Change Default Time Zone in PHP
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.