Many shared hosting environments automatically inject Cache-Control: no-store, no-cache, must-revalidate
and Pragma: no-cache
headers into PHP responses. This caching behavior is often configured at the server level, making it difficult to override when you only have FTP access to your site directory.
These default cache-prevention headers can:
- Negatively impact performance for static content
- Interfere with proper caching strategies
- Override your application's caching logic
Even without server access, you can modify these headers using directives in your .htaccess
file:
# Remove Cache-Control header if present
Header unset Cache-Control
# Remove Pragma header if present
Header unset Pragma
# Alternative: Set custom cache values
Header set Cache-Control "public, max-age=86400"
1. The Header
directive requires mod_headers
to be enabled (common on most hosts)
2. For PHP files specifically, add this conditional:
<FilesMatch "\.(php)$">
Header unset Cache-Control
Header unset Pragma
</FilesMatch>
After implementing, verify with:
curl -I https://yourdomain.com/example.php
Or using browser developer tools (Network tab).
If .htaccess modifications don't work, you can override headers in PHP:
<?php
header_remove('Cache-Control');
header_remove('Pragma');
// Or set custom headers:
header('Cache-Control: public, max-age=3600');
?>
If headers persist:
- Check for multiple .htaccess files in parent directories
- Confirm mod_headers is enabled (contact host if unsure)
- Clear any server-level cache that might be preserving old headers
Many developers encounter unexpected HTTP headers like Cache-Control: no-store, no-cache, must-revalidate
and Pragma: no-cache
being added to their PHP responses. This often happens in shared hosting environments where Apache's global configuration automatically adds these headers.
When you don't have access to Apache's main configuration files (httpd.conf
or apache2.conf
), the .htaccess
file becomes your best friend. This distributed configuration file lets you override server settings at the directory level.
To completely remove these headers, add this to your .htaccess
:
<IfModule mod_headers.c>
Header unset Cache-Control
Header unset Pragma
</IfModule>
If you want different caching behavior instead of complete removal:
<IfModule mod_headers.c>
Header set Cache-Control "max-age=86400, public"
Header set Pragma "cache"
</IfModule>
- Make sure
mod_headers
is enabled on your server - Place these directives before any rewrite rules
- Test with browser developer tools to verify changes
- Clear your browser cache when testing
If the headers persist:
- Check for multiple
.htaccess
files in parent directories - Verify your hosting provider allows header modifications
- Try adding
always
keyword:Header always unset Cache-Control
For more granular control, you can remove headers only for specific file types:
<FilesMatch "\.(php|html)$">
<IfModule mod_headers.c>
Header unset Cache-Control
Header unset Pragma
</IfModule>
</FilesMatch>