If your website serves different types of files, then its essential to set the right content type for each file type. Otherwise, the client browser may render it wrongly. For example, your raw text files with UTF8 characters may not be displayed properly. So let us take a look at how to set content type in Apache web server.
How to Set Content Type in Apache
Here are the steps to set content type in Apache.
1. Open Apache Config file
Open Apache config file in a text editor. You will find it at one of the following locations, depending on the type of installation and your linux distribution.
$ sudo vim /etc/apache2/httpd.conf
2. Set Content Type in Apache
Add the following lines to your Apache config file
#Set the correct Char set for all pages AddDefaultCharset utf-8 #for css, js, html AddCharset utf-8 .htm .html .js .css
First, add the default charset for all pages so they render properly. Then you can add charset for specific file extensions.
3. Restart Apache web server
Restart Apache web server to apply changes
$ sudo /etc/init.d/apache2 start [Debian or Ubuntu] # sudo apachectl restart [RHEL, CentOS or Fedora]
That’s it! Now Apache will automatically set the right content type header for all your file types.