How to set HTTP headers (for cache-control)?

HttpBrowser Cache

Http Problem Overview


How to enable browser caching for my site? Do I just put cache-control:public somewhere up in my header like this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
Cache-Control:public;
>

I am using the latest version of PHP developing on the latest version of XAMPP.

Http Solutions


Solution 1 - Http

To use cache-control in HTML, you use the meta tag, e.g.

<meta http-equiv="Cache-control" content="public">

The value in the content field is defined as one of the four values below.

Some information on the Cache-Control header is as follows

> HTTP 1.1. Allowed values = PUBLIC | PRIVATE | NO-CACHE | NO-STORE.
> > Public - may be cached in public shared caches.
> Private - may only be cached in private cache.
> No-Cache - may not be cached.
> No-Store - may be cached but not archived.
> > The directive CACHE-CONTROL:NO-CACHE indicates cached information should not be used > and instead requests should be forwarded to the origin server. This directive has the same semantics as the PRAGMA:NO-CACHE.
> > Clients SHOULD include both PRAGMA: NO-CACHE and CACHE-CONTROL: NO-CACHE when a no-cache request is sent to a server not known to be HTTP/1.1 compliant. Also see EXPIRES. > > Note: It may be better to specify cache commands in HTTP than in META statements, where they can influence more than the browser, but proxies and other intermediaries that may cache information.

Solution 2 - Http

You can set the headers in PHP by using:

<?php
  //set headers to NOT cache a page
  header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1
  header("Pragma: no-cache"); //HTTP 1.0
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

  //or, if you DO want a file to cache, use:
  header("Cache-Control: max-age=2592000"); //30days (60sec * 60min * 24hours * 30days)

?>

Note that the exact headers used will depend on your needs (and if you need to support HTTP 1.0 and/or HTTP 1.1)

Solution 3 - Http

As I wrote is best to use the file .htaccess. However beware of the time you leave the contents in the cache.

Use:

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

Where: 604800 = 7 days

PS: This can be used to reset any header

Solution 4 - Http

The page at http://www.askapache.com/htaccess/apache-speed-cache-control.html suggests using something like this:

> ### Add Cache-Control Headers > > This goes in your root .htaccess file but if you have access to > httpd.conf that is better.

> This code uses the FilesMatch directive and the Header directive to add Cache-Control Headers to certain files. > > # 480 weeks > > Header set Cache-Control "max-age=290304000, public" >

Solution 5 - Http

This is the best .htaccess I have used in my actual website:

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

##Tweaks##
Header set X-Frame-Options SAMEORIGIN

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

<IfModule mod_headers.c>
    Header set Connection keep-alive
	<filesmatch "\.(ico|flv|gif|swf|eot|woff|otf|ttf|svg)$">
		Header set Cache-Control "max-age=2592000, public"
	</filesmatch>
	<filesmatch "\.(jpg|jpeg|png)$">
		Header set Cache-Control "max-age=1209600, public"
	</filesmatch>
	# css and js should use private for proxy caching https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching
	<filesmatch "\.(css)$">
		Header set Cache-Control "max-age=31536000, private"
	</filesmatch>
	<filesmatch "\.(js)$">
		Header set Cache-Control "max-age=1209600, private"
	</filesmatch>
	<filesMatch "\.(x?html?|php)$">
        Header set Cache-Control "max-age=600, private, must-revalidate"
      </filesMatch>
</IfModule>

Solution 6 - Http

For Apache server, you should check mod_expires for setting Expires and Cache-Control headers.

Alternatively, you can use Header directive to add Cache-Control on your own:

Header set Cache-Control "max-age=290304000, public"

Solution 7 - Http

The meta cache control tag allows Web publishers to define how pages should be handled by caches. They include directives to declare what should be cacheable, what may be stored by caches, modifications of the expiration mechanism, and revalidation and reload controls.

The allowed values are:

Public - may be cached in public shared caches
Private - may only be cached in private cache
no-Cache - may not be cached
no-Store - may be cached but not archived

Please be careful about case sensitivity. Add the following meta tag in the source of your webpage. The difference in spelling at the end of the tag is either you use " /> = xml or "> = html.

    <meta http-equiv="Cache-control" content="public">
    <meta http-equiv="Cache-control" content="private">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache-control" content="no-store">

Source-> http://www.metatags.info/meta_http_equiv_cache_control">MetaTags</a>

Solution 8 - Http

OWASP recommends the following,

Whenever possible ensure the cache-control HTTP header is set with no-cache, no-store, must-revalidate, private; and that the pragma HTTP header is set with no-cache.

<IfModule mod_headers.c>
    Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
    Header set Pragma "no-cache"
</IfModule>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandrewView Question on Stackoverflow
Solution 1 - HttpCodemwnciView Answer on Stackoverflow
Solution 2 - HttpscunliffeView Answer on Stackoverflow
Solution 3 - HttpWilliamView Answer on Stackoverflow
Solution 4 - Httpangry kiwiView Answer on Stackoverflow
Solution 5 - HttpErich GarcíaView Answer on Stackoverflow
Solution 6 - HttpPeter ŠtibranýView Answer on Stackoverflow
Solution 7 - HttpKarthik N GView Answer on Stackoverflow
Solution 8 - HttpWon Jun BaeView Answer on Stackoverflow