Understanding Apache mod_headers

Updated November 2026 for Apache 2.4. The complete, current reference is Security Headers in THE Ultimate Htaccess.

Apache's mod_headers module is a powerful tool that allows administrators to manage HTTP headers in client-server communications. This module is essential for enhancing web security, managing caching, and controlling how web clients interact with server resources.

Key Features

  1. Header Manipulation: mod_headers enables you to add, modify, or remove HTTP request and response headers. This is crucial for ensuring that the correct headers are transmitted between the client and server.

  2. Conditional Operations: You can apply headers conditionally based on environment variables, request methods, or specific criteria within the request or response.

  3. Security Enhancements: By controlling headers such as Strict-Transport-Security, Content-Security-Policy, and X-Frame-Options, mod_headers helps improve the security posture of your web application.

  4. Caching Control: It allows you to set caching headers like Cache-Control, Expires, and ETag, which dictate how long content is cached by browsers and intermediary caches.

Basic Configuration

To use mod_headers, it must first be enabled. This is done in your main Apache configuration file (httpd.conf or apache2.conf, or a2enmod headers on Debian and Ubuntu). LoadModule cannot go in .htaccess, but once the module is loaded, every Header example below works in .htaccess.

Enabling the Module:

LoadModule headers_module modules/mod_headers.so

Adding a Header:

Header set X-Custom-Header "value"

Modifying a Header:

Header edit X-Custom-Header "old_value" "new_value"

Removing a Header:

Header unset X-Unwanted-Header

Conditional Header Application:

<If "%{REQUEST_URI} =~ m#^/specific/path#">
    Header set X-Conditional-Header "value"
</If>

Practical Use Cases

  1. Security Headers: Adding security headers to enhance the protection of your web application:

    Header always set Content-Security-Policy "default-src 'self'; frame-ancestors 'self'"
    Header always set X-Frame-Options "DENY"
    Header always set X-Content-Type-Options "nosniff"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
  2. Cache Control: Controlling how long content should be cached by the client:

    Header set Cache-Control "max-age=3600, public"
    # No hard-coded Expires date: it goes stale. mod_expires calculates it per request.
    
  3. Custom Headers for API: Setting custom headers to provide additional information in API responses:

    Header set X-API-Version "1.0"
    Header set Access-Control-Allow-Origin "*"
    

Best Practices

Conclusion

Apache mod_headers is an essential module for web administrators looking to fine-tune their server's HTTP headers. Whether enhancing security, managing caching strategies, or customizing responses, mod_headers provides the necessary tools to control HTTP headers efficiently and effectively.

Comments