Cache for images, php, js, and html - php

I want to cache all of my files but I can't figure out how to get it to work so that the tests approve. I have currently
<meta http-equiv="Cache-Control" content="private" />
<meta http-equiv="Expires" content="86400000" />
<meta http-equiv="Cache-Control" content="max-age=86400000" />
The last line I added just to test if having expires and max-age will help (it doesn't)
I was using http://www.webpagetest.org/, https://developers.google.com/pagespeed/#, and http://gtmetrix.com/
can anyone tell me simply how to make sure everything is cached privately? I have checked a bunch of other pages but no one gives legit HTML code. Please list actual code don't just tell me to use Cache-Control and expires and that like every other website I have seen uses. I really need example code in order to understand. Thank you for any help in advance. I am also using PHP so if doing it in a header() then that would work too.
Thank you very much
edit: I also tried using .htaccess in order to do it but that didn't work. I don't know if it was a setting with my server or what but it didn't change anything with the test.

When you specify an expiration time in an HTML document, it only applies to the actual document.
Assuming you have an Apache webserver with mod_expires enabled, you can create a file named .htaccess and include the following
ExpiresActive On
ExpiresByType image/gif 86400000
ExpiresByType image/png 86400000
ExpiresByType image/jpg 86400000
ExpiresByType image/jpeg 86400000
ExpiresByType text/html 86400000
ExpiresByType text/javascript 86400000
ExpiresByType text/plain 86400000

you can use .htaccess to cache your files.
#cache html and htm files for one day
<FilesMatch ".(html|htm)$">
Header set Cache-Control "max-age=43200"
</FilesMatch>
#cache css, javascript and text files for one week
<FilesMatch ".(js|css|txt)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
#cache flash and images for one month
<FilesMatch ".(flv|swf|ico|gif|jpg|jpeg|png)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
#disable cache for script files
<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>

Related

conditional downloading page contents (js, css)

How to tell the browser that if css or js files are in not modified (303) state than load it from cache otherwise get it from server?
I have this htacces so far, but this rule has to be overridden if there is any change:
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
# Set up caching on media files for 6 MONTH
<FilesMatch "\.(png|ico|css|js)$">
ExpiresDefault A604800
</FilesMatch>
http://httpd.apache.org/docs/2.0/mod/mod_expires.html
You can use the modified time instead of access time in the Expires rule.
<FilesMatch "\.(png|ico|css|js)$">
ExpiresDefault M604800
</FilesMatch>

Add Apache header if not set by application

I'm trying to control the caching of files within a certain directory. I want the default cache time to be 15 minutes, but I want to let the application change it if necessary. For example, I may have a PHP script that I want to refresh every 1 minute, so I'll set the cache-control headers within PHP for that script. But for all of the other files I just want the cache time to be 15 minutes, and some of those are static files, so I can't just set a default cache-time in PHP.
I currently have this in my Apache config:
<Directory />
Options FollowSymLinks
AllowOverride None
Header set Cache-Control "max-age=900"
</Directory>
This works great for 99% of the cases, where I just want a 15 minute cache. However, if my PHP script sets a cache-control header, then this setting will overwrite it.
I've looked at the documentation for mod_header and none of the settings (unset, add, append, etc.) seem to give me what I need.
Thanks in advance.
Take a look at mod_expires instead http://httpd.apache.org/docs/2.2/mod/mod_expires.html. The docs say that it won't overwrite headers created by your PHP script:
"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."
Here is an example config for mod_expires:
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault A600
ExpiresByType image/gif "access plus 1 day"
ExpiresByType image/jpeg "access plus 1 day"
ExpiresByType image/png "access plus 1 day"
ExpiresByType image/x-icon "access plus 1 day"
<FilesMatch "\.(php|php4)$">
ExpiresByType text/html "now"
</FilesMatch>
</IfModule>
Taken from http://howto.gumph.org/content/reduce-webserver-bandwidth/
Good luck!
According to php manual
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
By sending the headers above, you should override any settings that may otherwise cause the output of your script to be cached.

website update cache

If you have a live website (using PHP,MySQL,JS,CSS) and say you change some functionality. What is the best way to make sure, that when a user visits the site, the changed pages are updated and not loaded from cache?
For that reson there is file loaders made for. When you load css, js or any other file, you must set cache control header. You can do it by editing .htaccess or with php.
.htaccess demo
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A300
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A3600
ExpiresByType text/css A3600
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType text/plain A300
ExpiresByType application/x-shockwave-flash A604800
ExpiresByType video/x-flv A604800
ExpiresByType application/pdf A604800
ExpiresByType text/html A300
</IfModule>
PHP demo:
$offset = 3600 * 24;
$expire = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
header($expire);
Via HTML, in the Head section put...
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
You can't really control the policy each browser, if it complies to the standards, but what commonly happens is the browser does download the header from each HTML before deciding whether or not it should be loaded from cache or re-downloaded.
Check RFC HTTP 1.1 Header Field for more details (under cache control in particular)
Unless you have some form of caching the user will be requesting data and checking if it has been updated. Usually your PHP is not cached unless you have active caching.
Your JS and CSS would have to be set via HEADERS under say Apache (or whatever web server you utilize).
You can use the window.applicationCache.swapCache(); and window.location.reload(); to force reload the page once javascript confirms that there's an updated version of the site.
A better option may be to prompt the user in some way, letting them know that the site updated. That way, the page doesn't just suddenly reload.
function updateCache(){
window.applicationCache.swapCache();
window.location.reload();
}
window.applicationCache.addEventListener("updateready", updateCache, false);

Leverage browser caching with php?

I was checking google page speed tool # http://pagespeed.googlelabs.com and my site point was 88. It suggest me to use Leverage browser caching for the site. I searched stackoverflow about it but all it was about htaccess, my hosting doesn't let me to use htaccess, how can I make it in PHP without htaccess?
htaccess codes were
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>
If your hoster does not support .htaccess nor configuring the webserver with other methods, you would need to implement the whole HTTP stack into your own application to offer configuration of your own.
That means sending the appropriate headers for the files in question next to the files itself. You would need to map those files onto commands your application (which is normally done with .htaccess + Mod_Rewrite as well).
Shortly said, you would need to deliver everything by PHP scripts that set the headers in question. However this has the downside that PHP needs to process everything which will have a drawback on speed compared to static file delivery by the webserver. So I can not really suggest you to do it that way. It's much easier to just get a proper webhoster (or to upgrade your package) to get the features you're looking for before re-inventing the wheel. So getting some .htaccess support is probably the most easy way.
As an alternative but somewhat similiar, you can consider to put the static files onto another host that provides the features you need (e.g. a CDN) and leave the core application on the current webhost, but I assume this only makes things more complicated than it does help you easily.
After doing a day of research i get this solution for Leverage browser cashing with .htaccess file.
Remember mod_expires and mod_headers should be open in server
Just put the on the .htaccess file.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(css)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</FilesMatch>
<FilesMatch "\\.(x?html?|php)$">
Header set Cache-Control "max-age=600, private, must-revalidate"
</FilesMatch>
Header unset ETag
Header unset Last-Modified
</IfModule>
you can't do it without permissions to do anything via htaccess or ACP

Cache Expiration On Static Images [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a php site using html5 run on a linux server on godaddy. I need to set a cache expiration date on static images and a css file. I also need to gzip a css file and cannot seem to find the correct syntax for either. I am not sure if I am making this too complicated or what. Am I correct to think I can get this done with an expire header in the top of my php pages? I feel lost and I know what i am doing! I do not have control over the server.
This is definitely possible if you have control over the server, and maybe possible through .htaccess if you're on shared hosting.
Try these SO questions:
How to gzip my files
How to set the cache expiry period of a whole folder of jpgs using .htaccess
Why isn’t my javascript & css caching ? (With full examples)
Godaddy can be extremely frustrating. I have been seeking a means of using expires header with Godaddy hosting for some time and haven't found a solution yet.
I have the following in htaccess ('A2592000' indicates 1 month) and it works with other hosts but not Godaddy :(
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000
I had completely same problem. Disaster with GoDaddy. Never more their hosting :(
But I found solution:
https://support.godaddy.com/help/article/6908/enabling-mod_expires-with-your-hosting-account?locale=en
I do not know how you manage your hosting but I use FileZila to connect to FTP. So after I connect to FTP you can rewrite .htaccess file :) so download original one and add this code after:
<IfModule mod_expires.c>
# Activate mod_expires for this directory
ExpiresActive on
# locally cache common image types for 7 days
ExpiresByType image/jpg "access plus 7 days"
ExpiresByType image/jpeg "access plus 7 days"
ExpiresByType image/gif "access plus 7 days"
ExpiresByType image/png "access plus 7 days"
# cache CSS files for 24 hours
ExpiresByType text/css "access plus 24 hours"
I hope this will help you. (it helped me:D)
You're looking for something like this:
Header set Cache-Control "max-age=2678400"
Where max-age is set in seconds.
Additional, if your contents is still not being cached, read my post at Why isn't my javascript & css caching? for additional cache-config-magic.
You can try this
<FilesMatch "\.(jpg|png|gif)$">
ExpiresDefault A0
Header set Cache-Control "max-age=0, no-cache, must-revalidate"
</FilesMatch>
Which mean, that expiration date is in access moment and set headers to 0 values.
As You can see, here You can add more filetypes
/via http://blog.simplemediacode.com/cache-expiration-on-static-images-and-content-with-htaccess/

Categories