.htaccess ExpiresDefault 1 day - apache

If I add only these 2 lines to the .htacces file, will all the files on the website be kept in the cache for 1 day?
ExpiresActive On
ExpiresDefault "access plus 1 day"
Is it enough and it works?

ExpiresActive On
ExpiresDefault "access plus 1 day"
If you have no other overriding directives then these directives do indeed instruct the browser to cache the response for 1 day, by sending back the appropriate Expires and Cache-Control: max-age HTTP response headers. (Expires is required for old browsers. All modern browsers will use the Cache-Control header instead.)
Is it enough and it works?
Whether it is "enough" is entirely dependent on the nature of your site. Depending on your site, some responses perhaps shouldn't be cached at all, whilst other static resources should be cached for much longer.
And it's entirely possible you have other directives or even a front-end proxy or CDN that overrides these response headers.

Related

what is the correct caching directive syntax for mod_expires?

We're considering using the following caching directive in our .htaccess file
ExpiresByType text/html "access"
Is that valid syntax? And what does it mean?
My understating (and testing, in 4 major browsers has shown) is that when a client accesses the HTML file, it will remain in it's cache and be pulled from it's cache, until we modify the HTML file on our server, at which time the client will pull the file from our server and use the modified/updated copy.
The apache docs for mod_expires clearly shows the additional arguments are optional.
ExpiresByType text/html "access"
Well, it's "valid", but this effectively expires the cache instantly (unless there is another caching mechanism in place - see below).
The ExpiresByType directive simply controls the Expires and Cache-Control: max-age headers. The above directive results in the Expires header being set to now (the current time) and max-age: 0 - in other words a zero cache time.
when a client accesses the HTML file, it will remain in it's cache and be pulled from it's cache, until we modify the HTML file on our server
This is not related to the ExpiresByType directive (as mentioned above), which explicitly states "access", not "modification".
What you are probably seeing is caching as a result of the Last-Modified (Response) header and the If-Modified-Since (request) headers, but this is quite separate and not controlled by mod_expires.

Remove the Exipre header in Linux apache server

I am getting Expire Header as Thu, 19 Nov 1981 08:52:00 GMT on my webpage how i can remove expire header using apache.
Thanks
Thanigaivelan
In your .htacess:
ExpiresActive Off
It is not good to disable this complete.
Also take a look at mod_expires.
This module controls the setting of the Expires HTTP header and the
max-age directive of the Cache-Control HTTP header in server
responses. The expiration date can set to be relative to either the
time the source file was last modified, or to the time of the client
access.
These HTTP headers are an instruction to the client about the
document's validity and persistence. If cached, the document may be
fetched from the cache rather than from the source until this time has
passed. After that, the cache copy is considered "expired" and
invalid, and a new copy must be obtained from the source.
To modify Cache-Control directives other than max-age (see RFC 2616
section 14.9), you can use the Header directive.
When the Expires header is already part of the response generated by
the server, for example when generated by a CGI script or proxied from
an origin server, this module does not change or add an Expires or
Cache-Control header.
ExpiresByType text/html "access plus 1 month 15 days 2 hours"
ExpiresByType image/gif "modification plus 5 hours 3 minutes"

apache ExpiresDefault - just doesn't work

we all know that setting expire date for static files is very useful and the way i found to do so is through the ExpiresDefault property in a .htaccess file but it just doesn't work. I am using YSlow and page_speed to test the HTTP response, but it just keep on telling me that I need to set an expire date for my JS, CSS and images to speed up my application.
I tried to test ExpiresDefault "access plus 1 year" , locally ( apache server ) and on the web ( hostgator reseller account. if I have any options to make use of !?! ) but it just doesn't work. And it also doesn't return 500 error.
I feel like I am missing the main point or doing it wrong. How the whole thing should be done ?
First, identified mod_expires is compiled inside your apache.
Try
PATH_TO_YOUR/httpd -M | sort /* <-- look for expires_module*/
And your apache configuration should be like
ExpiresActive on
ExpiresDefault "access plus 1 year"
And always restart apache after configuration changed

ExpiresActive On .htaccess explanation

[i]ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
BrowserMatch "MSIE" brokenvary=1
BrowserMatch "Mozilla/4.[0-9]{2}" brokenvary=1
BrowserMatch "Opera" !brokenvary
SetEnvIf brokenvary 1 force-no-vary[/i]
saw this code in a sample .htaccess recently.
Presumably ExpiresByType sets an expiry time on images - is that related to the visitors browser cache? and what does A2592000 translate to?
and what does the "brokenvary=1" imply? I gather it's looking for a UserAgent, but then what?
thanks!
Presumably ExpiresByType sets an expiry time on images - is that related to the visitors browser cache?
Yes. mod_expires allows an easy setup of expiration rules based on the type.
But the expiration time specifies only the freshness time of a certain response. This does not necessarily mean that the response is cacheable. But in general, any successful response is cacheable unless there are restrictions:
Unless specifically constrained by a cache-control (section 14.9) directive, a caching system MAY always store a successful response (see section 13.8) as a cache entry, MAY return it without validation if it is fresh, and MAY return it after successful validation.
So unless you specify the response not to be stored at all (i.e. using no-store), the response may be stored by both public caches (shared caches) and private caches (local caches).
And what does A2592000 translate to?
The freshness time of a response can be expressed using either an absolute time value (e.g “2010-10-09”) or a relative time value (e.g. “tomorrow”). The date format A2592000 uses a latter time value as A denotes access time and 2592000 is the number of seconds that are added. So A2592000 means “2592000 seconds from the time of access on”.
And what does the "brokenvary=1" imply? I gather it's looking for a UserAgent, but then what?
Apache has some special purpose environment variables where force-no-vary is one of them:
This causes any Vary fields to be removed from the response header before it is sent back to the client. Some clients don't interpret this field correctly; setting this variable can work around this problem. Setting this variable also implies force-response-1.0.
Now the Vary header field is used to specify a list of header field names the server used to select the response among multiple representations:
A server SHOULD use the Vary header field to inform a cache of what request-header fields were used to select among multiple representations of a cacheable response subject to server-driven negotiation.
So if you’re using content negotiation and a requested generic URL like /document.html is requested and there are multiple representations of that resource (e.g. in English and German) and your server selects the German variant as Accept-Language states the value de, the server would include a Vary field containing Accept-Language to let the caches know that the selection was based on the value of Accept-Language.
But some user agents don’t get this right. And in that cases the Vary header field should not be sent that can be done by setting the special purpose environment variable force-no-vary.
ExpiresByType is an Apache Directive of the mod_expires module that generates 'Expires' and 'Cache-control' http response headers. These headers tell a browser that it is allowed to cache the resource for a specific amount of time.
From the documententation ( http://httpd.apache.org/docs/2.0/mod/mod_expires.html ) :
'A' means the client's access time should be used.
An example from the same page might explain things:
# enable expirations
ExpiresActive On
# expire GIF images after a month in the client's cache
ExpiresByType image/gif A2592000
# HTML documents are good for a week from the
# time they were changed
ExpiresByType text/html M604800

With Apache httpd, how do I configure no caching for a given UserAgent?

I have Apache HTTPD configured to add a cache header to requests for most static content:
ExpiresActive On
ExpiresDefault "access plus 1 year"
# Force JNLP and BSH files to expire immediately so updates are checked for
# and seen (We need this so we see changes in the dynamic content in both)
ExpiresByType application/x-java-jnlp-file "now"
ExpiresByType application/x-bsh "now"
How can I disable this caching for any request where the UserAgent contains the string JNLP? When the request comes from the User Agent JNLP (for example "User-Agent: JNLP/6.0 javaws/1.6.0_12 (b04) Java/1.6.0_12") I don't want any Cache-Control or other cache-related headers on the HTTP response.
I can find configuration examples for several things based on user agent, but I cannot figure out how to configure caching depending on the user agent.
Your ExpiresByType directive looks like a good idea... if that's not enough, then try using BrowserMatch:
BrowserMatch JNLP ua_is_jnlp
This sets the environment variable ua_is_jnlp to some value for any request whose user agent header contains the string JNLP (there is also a case-insensitive variant, BrowserMatchNoCase). Then you can clear any caching headers with
Header unset Cache-Control env=ua_in_jnlp
Header unset Expires env=ua_in_jnlp
although if you want to disable caching entirely, you'd be better off setting
Header set Cache-Control no-cache env=ua_in_jnlp
Header set Expires 0 env=ua_in_jnlp