How to change HTTP headers using .htaccess files

This article shows how to edit the HTTP headers that Apache sends to a client directly.

Modifying HTTP headers

The mod_headers module is included in the Apache setup on HostGinger Hosting servers. This means that using the Header directive in an .htaccess file, you may quickly add, alter, and delete HTTP response headers.

Adding a header

Use the set or append options to add a custom header to the HTTP response headers. The set option replaces any heading with the same name with the supplied header. If the header does not already exist, the append option creates it. However, if the header already exists, the append option adds to the existing header value.

For example, the following sample configuration demonstrates how to set a header and append to a header:

<IfModule mod_headers.c>
  Header set Animal cow
  Header set Animal monkey
  Header append Animal camel
</IfModule>

In this example, the Animal header is first set to cow. But because the next header option is also set, the Animal header is reset to monkey. Lastly, camel is appended to the Animal header. This generates an HTTP response header that resembles the following:

HTTP/1.1 200 OK
Date: Tue, 24 Jun 2014 15:23:33 GMT
Server: Apache
Animal: monkey, camel
Editing a header

To edit a header, use the edit option. With the edit option, you can use regular expressions to do search-and-replace operations on header values.

For example, the following sample configuration demonstrates how to do a (very) simple search-and-replace operation on a header:

<IfModule mod_headers.c>
  Header set Animal monkey 
  Header append Animal camel
  Header edit Animal "camel" "llama"
</IfModule>

In this example, camel is replaced by llama. This generates an HTTP response header that resembles the following:

HTTP/1.1 200 OK
Date: Tue, 24 Jun 2014 15:42:21 GMT
Server: Apache
Animal: monkey, llama
Removing a header

To completely remove a header from an HTTP response, use the unset option.

The following sample configuration demonstrates how to remove a header:

<IfModule mod_headers.c>
  Header unset Animal
</IfModule>
Was this answer helpful?

Related Articles

How to enable and disable directory index listings using an .htaccess file?

To enable index listings for a directory add the following line to the .htaccess file: Options...

How to protect a website folders using .htaccess

The .htaccess file is a configuration file used by Apache-based web servers. Directives in...

How to use an .htaccess file to alter the default directory index page?

On HostGinger Hosting servers, by default, when users access a URL that requests a directory,...

What is .htaccess files?

An .htaccess file is a plain-text configuration file that you can use to alter the settings of...

How to protect .htaccess file contents?

By default, everybody has access to an .htaccess file's contents. Due to the fact that it exposes...