Knowledge Base

Apache Header Examples

Apache mod_headers can set response headers in server configuration or an allowed .htaccess file. These examples show a small baseline, not a complete security policy.

A small baseline

<IfModule mod_headers.c>
    Header onsuccess unset X-Content-Type-Options
    Header always set X-Content-Type-Options "nosniff"
    Header onsuccess unset Referrer-Policy
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

The unset/set pattern removes a competing value from Apache's default header table and sets the desired value in its always table. Always also covers error responses. Confirm that replacing an existing value is appropriate for your deployment.

Prerequisites and hosting layers

The module must be enabled. .htaccess use also needs the host to permit the relevant FileInfo overrides. The IfModule wrapper avoids an error if mod_headers is absent, but then these rules do nothing: inspect the returned response.

On shared hosting, the application, hosting cache, proxy, or CDN may still change the public response. Check the final HTTPS response rather than assuming .htaccess is the last layer.

HSTS on HTTPS

# Inside the HTTPS virtual host, after HTTPS is verified:
Header onsuccess unset Strict-Transport-Security
Header always set Strict-Transport-Security "max-age=300"

This short rollout example belongs in the HTTPS context. Do not add subdomain coverage or preload until the affected services are ready. See the HSTS guide.

Verify the response

Test a successful page, a redirect, and an error response. Confirm one intended value per policy and correct Content-Type values alongside nosniff. Add application-specific CSP and caching rules only after reviewing the configuration guides.

Sources

Copyright ©2026 MILAN INNOVATIONS LLC. All rights reserved.