Search Our Database

HTTP Header | Configuring HTTP Cache-Control Headers Using .htaccess

Last updated on |

Introduction

HTTP response headers provide information about website content, caching behaviour, and security policies. This article focuses on configuring the Cache-Control response header using an Apache .htaccess file.

  • Recognise commonly encountered HTTP response headers.
  • Understand the purpose of common Cache-Control directives.
  • Configure and verify a caching policy for public static website files.

HTTP Response Headers

The following table provides an overview of common HTTP response headers. The headers returned vary by website and server configuration.

HTTP Header Description
Content-Type Identifies the media type of the response, such as text/html.
Content-Length Indicates the response body size in bytes, when provided.
Cache-Control Controls how browsers and shared caches may store and reuse a response.
ETag Identifies a version of a resource for conditional requests.
Last-Modified Indicates when the server believes a resource was last modified.
Expires Specifies a date and time after which a cached response is considered stale.
Vary Lists request headers that influence which representation is selected.
Strict-Transport-Security Instructs supported browsers to use HTTPS for the specified period.
Content-Security-Policy Restricts which resources or content a browser may load or execute.
X-Frame-Options Restricts whether a page may be displayed inside a frame.
X-Content-Type-Options With nosniff, instructs browsers not to MIME-sniff certain responses.
X-Robots-Tag Provides indexing or crawling instructions to search engines that recognise the header.
Server May identify the software that handled the request.

Note: HTTP/2 200 represents the response status in curl output; it is not an HTTP response header field.

Cache-Control Directives

The Cache-Control response header defines rules for storing, reusing, and revalidating website content.

Cache-Control Value Description
no-store Instructs caches not to store the response.
no-cache Allows storage but requires revalidation before the stored response is reused.
private Allows private-cache storage, but not storage by shared caches.
public Explicitly permits shared-cache storage, subject to other applicable rules.
max-age=3600 Specifies a freshness lifetime of 3,600 seconds (one hour).
public, max-age=3600 Permits shared caching with a one-hour freshness lifetime.

 

Example: The following response permits eligible caches to reuse a public resource while it remains fresh for one hour.

Cache-Control: public, max-age=3600

Other headers, such as ETag and Last-Modified, support revalidation.

A response without Cache-Control is not necessarily uncached; some caches may apply heuristic caching.

 

Prerequisites

  • Access to the website’s .htaccess file using Control Panel File Manager, FTP, or SSH.
  • An Apache web server with mod_headers available and permission to use the Header directive in .htaccess.
  • A backup of the existing .htaccess file before making changes.
  • A publicly accessible static file to use for verification.

Important Note ⚠️:Impact

  • Long caching durations may cause visitors to see outdated CSS, JavaScript, or images.
  • Invalid or disallowed .htaccess directives can result in an HTTP 500 error.
  • CDNs, reverse proxies, and applications may apply additional caching rules.

 

Best Practices

  • Back up the existing configuration and review any pre-existing caching directives.
  • Apply the example only to public static resources, not authenticated or personalised content.
  • Confirm that Apache serves the files being tested; content served directly by Nginx may not use Apache .htaccess rules.

Step-by-Step Guide

Step 1: Locate and back up the .htaccess file

  • Open the website’s document root and locate .htaccess. Enable the display of hidden files if needed.
  • Save a backup copy before editing.
  • If the file is managed by an application such as WordPress, avoid modifying its automatically generated rules, use WordPress plugins.

 

Step 2: Configure Cache-Control for public static files

  • Add the following configuration to .htaccess to specify a one-hour freshness lifetime for common public static assets:
<IfModule mod_headers.c>
<FilesMatch “\.(css|js|png|jpe?g|gif|svg|webp|ico)$”>
Header set Cache-Control “public, max-age=3600”
</FilesMatch>
</IfModule>
  • FilesMatch limits the rule to matching file extensions.
  • Header set specifies the outgoing Cache-Control value.
  • IfModule skips the enclosed rule when mod_headers is unavailable; it does not enable the module.

 

Step 3: Save the configuration

  • Save .htaccess and confirm the website remains accessible. An Apache restart is generally not required for changes to an enabled .htaccess file.

 

Step 4: Verify the HTTP response headers

  • Run curl against an existing static file covered by the rule:
curl -sS -D – -o /dev/null https://www.example.com/assets/style.css

 

Expected result: If the rule applies to the requested file, the response should contain a header similar to:

HTTP/2 200
content-type: text/css
cache-control: public, max-age=3600

Replace the example URL with a real file on your website.

The response status and other headers may differ. HTTP header names are case-insensitive.

 

Common Issues

Issue Recommended Check
Cache-Control is missing Confirm mod_headers is available, the Header directive is permitted, and the requested file matches the rule.
Header differs from the configured value Review other .htaccess rules, application settings, reverse proxies, or CDN configuration.
Duplicate Cache-Control headers Review overlapping header rules and application-generated headers before making further changes.
HTTP 500 error after editing Restore the backup and review the Apache error log for invalid or disallowed directives.
Visitors see outdated content Check the freshness lifetime and any additional browser or CDN caching.

Conclusion

Cache-Control helps administrators define how public website resources are cached and revalidated.

Apply a suitable policy in .htaccess and verify the returned HTTP headers.

 

Through technology, we learn. Through sharing, we connect.

Explore more guides at https://kb.ipserverone.com/

For technical support / service enquiries, please reach out to support@ipserverone.com for assistance.