"Add Expires Headers" - what about HTML? - http-headers

Probably quite a basic question but I want to be 100% sure...
It's recommended to place "Add Expire Header" rules in your htaccess file, but my question is that, the HTML text within my site changes very often, i.e. written content - so, just to be clear, adding the below is completely fine given that my content (text) changes regularly?
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##
I want repeat visitors to see my fresh content of course, hence why I'm asking
Thanks

No, that's not completely fine given your requirement:
I want repeat visitors to see my fresh content
If you serve an HTML page with headers that indicate that the client can cache that response until (now + two days), then if the user refreshes that page, for the next two days the browser will return the cached HTML.

Related

Request after 10 minutes takes more time to load

I Am facing an issue in web application.
First time the page loads in 5 to 6 seconds. If i keep the browser idle for 10 minutes and then again request or click a tab on web application takes 10 to 15 seconds to load the content.
In this 10 minutes if we are accessing one or the other contents then it wont take much time to load. But if we keep the browser idle for 10 minutes and then try to access it its taking more time to load.
I am using Apache server with default apache configurations.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 day"
ExpiresByType image/jpeg "access plus 1 day"
ExpiresByType image/gif "access plus 1 day"
ExpiresByType image/png "access plus 1 day"
ExpiresByType text/css "access plus 1 day"
ExpiresByType application/pdf "access plus 1 day"
ExpiresByType text/x-javascript "access plus 1 day"
ExpiresByType application/x-shockwave-flash "access plus 1 day"
ExpiresByType image/x-icon "access plus 1 day"
ExpiresDefault "access plus 1 day"
#ExpiresDefault "access 1 month"
</IfModule>

Leverage browser caching on Amazon EC2

This is my first time using Amazon AWS for any hosting and I've uploaded my usual code, below, to help with browser caching and it seems that tools like GT Metrix and Google Page speed are not seeing it work.
<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/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
Any ideas if I need to enable anything on Amazons side for this to work?
Thanks in advance.
After some investigation it turned out that the Apache modules needed for this were not installed on our instance.
What you need to do is ssh into your server as a root user and run the following command
Check which modules are installed using this command
apache2ctl -M
and looking for expires_module. It's probably not there.
enable browser caching
sudo a2enmod expires
restart apache
sudo service apache2 restart

Caching compiled css and js and clears cache on changes in files

I have been going through lots of article, where a proper example for caching mechanism is not penned down. All tutorials describes about that and a single line demo which, I guess, never really optimizes. I have compiled css and js using grunt.
I have included
<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 application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
in my .htaccess file.
This, actually says, to cache the content and whenever any changes occur force the browser to use new files.
I do not actually see any changes in DOM Loaded content or in Load Time and everytime, the compiled css and js takes files from server and caches after 5-7 reloading. I guess the cache, which it performs, uses default browser cache.
So How would I use a caching mechanism so that it serves pages quickly and clears the cache when any changes effects the css and JS?
I am using PHP as my application. A better example for doing that with code would be really helpful.

Setting ExpiresByType for entire server

I run a server with several websites on it. I would like to implement default cache control behavior for all of these websites. Can I just do this by adding the following code to httpd.conf or must I make the changes in the configuration of each virtual host separately?
<IfModule mod_expires.c>
ExpiresActive on
# Your document html
ExpiresByType text/html "access plus 12 hours"
# Media: images, video, audio
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# CSS and JavaScript
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
# Default
ExpiresDefault "access plus 1 month"
</IfModule>
Documentation can be found in the Apache HTTP Server web site. In the "Documentation" section on the left pane, click on your server version. I'll assume 2.4. Since you are looking for reference on specific directives, you can click on the Directives link. You'll get an alphabetical index.
I won't copy the complete information, just a little sample for ExpiresActive. The important bit is the top table:
Description: Enables generation of Expires headers
Syntax: ExpiresActive On|Off
Default: ExpiresActive Off
Context: server config, virtual host, directory, .htaccess
Override: Indexes
Status: Extension
Module: mod_expires
At "Context" we can read that the directive can be set at several places, including server config. So this answers your question: in theory, it should work. You should check the rest of the directives in order to make sure (or just test it).

Implementing cache control using .htaccess on Apache server

okay, I'm still trying to get my head around some of the caching stuff and I have gone through a couple of examples I could find on Google. I have added the following code to my .htaccess file:
### activate mod_expires
ExpiresActive On
### Expire .gif's 1 month from when they're accessed
ExpiresByType image/gif "access plus 3 months"
ExpiresByType image/png "access plus 3 months"
ExpiresByType image/jpg "access plus 3 months"
ExpiresByType text/javascript "access plus 3 months"
Using the Chrome audit tools and the YSlow Firebug tool, it looks like this is caching some of my images/files, but not by far all of them. I still have a list of files (.jpg, .js and .css - I know I've not set the css files to cache here) that aren't caching. The message in the Chrome audit simply states The following resources are missing a cache expiration. Resources that do not specify an expiration may not be cached by browsers:
some of the images that aren't caching are background images, others are part of a js gallery and they're being called via the JS - could that be affecting why they aren't caching?
Sorry I can't give a link to the code - the sites still under wraps and limited to client view only.
Thanks in advance!
It looks like you've written the MIME-types wrong:
# enable expirations
ExpiresActive On
ExpiresDefault "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/pjpeg "access plus 1 week"
ExpiresByType text/javascript "modification plus 1 week"
ExpiresByType application/javascript "modification plus 1 week"
ExpiresByType text/css "modification plus 1 week"