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.
How To Set Default Charset to UTF-8 Encoding in Apache using htaccess
Here are the steps to set default charset to UTF-8 encoding in Apache using htaccess.
1. Open htaccess file
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
Also Read : How to Enable mod_rewrite in XAMPP, WAMP
2. Set Default Encoding to UTF-8
Add the following to .htaccess file.
This will be applicable for all files served by Apache web server.
If you want to apply UTF-8 encoding only for specific file types, use the filesMatch directive.
<filesMatch "\.(html|css|js)$"> AddDefaultCharset UTF-8 DefaultLanguage en-US </filesMatch>
Also read : How to Change Default Index Page
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.