Setup HTTP expires headers using PHP and Apache

Setup HTTP expires headers using PHP and Apache

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Setup HTTP expires headers using PHP and Apache

Managing browser caching is a critical component of performance optimization, yet it often introduces complexity when mixing server-side logic with static file handling. You might be using an auto_prepend_file to serve resources gzipped dynamically, which handles variable content well, but this approach complicates the strategy for setting HTTP Expires headers. These headers tell browsers when a resource is no longer valid, allowing you to reduce bandwidth usage and latency by serving cached assets without hitting the server every time.

The core challenge here is that PHP scripts run on every request, whereas static assets like images or CSS files need to be served directly by Apache with specific cache directives. If you try to set Expires headers entirely within your PHP auto_prepend_file, you risk applying those headers to dynamic content (like HTML pages) which should often have a much shorter cache duration or use no-cache. Furthermore, setting these headers in PHP does not affect static files that Apache serves directly before or after the script executes.

To maximize HTTP caching effectively, you need a hybrid approach where Apache handles static assets and PHP handles dynamic content with appropriate logic. For static resources, the most robust method is to configure the .htaccess file (or your main httpd.conf) using the mod_expires module. This allows you to define cache lifetimes based on file types without interfering with your PHP application's rendering logic.

Here is a typical configuration for .htaccess that applies aggressive caching to static assets while leaving dynamic content untouched:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/json "access plus 1 day"
</IfModule>

This configuration instructs Apache to send Expires headers for specific MIME types, ensuring that browsers cache images and stylesheets for extended periods. The trade-off here is that you must ensure your web server version supports mod_expires. If you are on a newer Apache version or running in a containerized environment, this module might be disabled by default.

However, if you require more granular control based on file names rather than MIME types, mod_expires alone isn't enough. In that case, you can use the mod_headers module to set custom headers for specific paths or conditions. This is useful if you want to exclude certain files from caching or apply different rules to a subdirectory:

<IfModule mod_headers.c>
    Header set Cache-Control "max-age=2592000, public"
    
    # Optional: Exclude specific files from caching
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^images/(.*\.(png|jpg|jpeg|gif))$ /images/$1 [L]
</IfModule>

When working with PHP to handle dynamic content, you should avoid setting Expires headers directly in the auto_prepend_file. Instead, it is better to set them at the very top of your main entry script or use a middleware pattern if you are using a framework like Laravel. This ensures that the header is set before any output is sent and applies only to the dynamic response.

One common pitfall is ignoring the Cache-Control directive in favor of Expires. Modern browsers prioritize Cache-Control over Expires. If you want to ensure maximum compatibility and control, especially for sensitive data or frequently changing content, you should set both headers or rely solely on Cache-Control with a max-age value. For example, Cache-Control: public, max-age=31536000 is generally more reliable than relying on Expires.

Finally, remember that setting these headers requires server-level access. You cannot configure Apache directives like mod_expires from within the PHP code alone without using specific modules or output buffering functions that might not cover static files served by the web server directly. If you are stuck, check your Apache documentation for version-specific module availability, as some modern distributions bundle these modules differently than legacy setups.

Our network