How to Set Content Type in Apache


set content type in apache

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.

  • /etc/apache2/httpd.conf
  • /etc/httpd/httpd.conf
  • /etc/httpd/conf/httpd.conf

 


$ 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.

In the above case, the javascript files will have content type “application/javascript” while css files will have content type “text/css” and overwrite the default charset.

 

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.

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!