Cache Expiration On Static Images [closed] - php

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/

Related

Create cache control for images in php

I run on a Windows server and want to cache different resources for different time periods: some stuff never changes, and other stuff changes every few weeks.
If I had Apache, I could use .htaccess to set up a list of commands like this (actually, this is what I used last year):
ExpiresByType image/jpg "access plus 100 days"
ExpiresByType image/gif "access plus 100 days"
ExpiresByType image/jpeg "access plus 100 days"
ExpiresByType image/png "access plus 100 days"
ExpiresByType text/javascript "access plus 100 days"
But I don't. IIS has nothing that I can see that's so specific, so perhaps I need to do this with PHP.
I've read a whole host of articles, but none give specific advice on how to achieve the same result as the .htaccess solution above.
Ideally, make it page-specific, which is where PHP might come in ... maybe some statements at the top of each page?
Any ideas?

How add Expires headers on Css.php file (Scaffold)

I use SocialEngine for my website, I want to speed my website so I have use Gtmetrix and PageSpeed to see what I can improve
I see that Add Expires headers have 0/100 ..
I have do research but how I can add on Exprires headers my CSS that look like this ..
https://www.exemple.com/application/css.php?request=application%2Fthemes%2Fmytheme%2Ftheme.css&c=2174&pageStart=332433&pageEnd=85951
https://www.exemple.com/application/css.php?request=application%2Fthemes%2Fmytheme%2Ftheme.css&c=2174&pageStart=667827&pageEnd=345656
https://www.exemple.com/application/css.php?request=application%2Fthemes%2Fmytheme%2Ftheme.css&c=2174&pageStart=1019386&pageEnd=6576458
How I can do ? Thank you
My .htaccess:
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year”
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access 1 month”
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
Themes /application/themes/* We utilize a framework called CSS
Scaffold which makes editing your community's theme a simple process.
Each theme is stored in its own directory within
/applications/themes/. A default theme is automatically loaded when
you first install SocialEngine. Each theme contains two files:
constants.css and theme.css.
At the top of constants.css, you'll find a series of global CSS
settings (called "constants"). You can edit these to adjust the
colors, fonts, and other styles throughout your entire community.
The other file, theme.css, contains more specific styles that are used
throughout your community. Many of these styles inherit values from
constants.css. If you want to override any of the default styles on
your community, you can edit them here. If they aren't present in
theme.css (and are being loaded from outside the theme itself), you
can override them by adding new styles to the bottom of theme.css.
More information about how to work with CSS Scaffold is available on
Github or Google for CSScaffold for videos and tutorials.
You already have configured
ExpiresByType text/css "access 1 month”
so CSS files should have the correct header.
It seems, that application/css.php delivers the appropriate CSS file. In order to trigger the expires header, the script must also set the correct mime type for the CSS files returned.
This can be accomplished with a header of Content-Type
header('Content-Type: text/css');
Alternatively, you could also add your own Expires header, e.g.
$plus_month = date('r', strtotime('+1 month'));
header("Expires: " . $plus_month);

How to cache images for a year but html / php for only 24 hours

I'm new here so sorry if this is in the wrong category or something like that. Now getting to my problem. I have a website: www.windowstaan.pe.hu which also opens on a mobile app so in order to make it load fast I have enabled caching in .htaccess and here's its code:
## 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 day"
ExpiresByType text/html "access 1 day"
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 ##
Now what happens here is that any page I load in the browser can also be loaded offline which is what I wanted, but if I make any changes to the html then I'm not sure how its going to update here.
My html files are saved as index.php, PHP because they have some PHP code in them mainly include header, footer and a small mysql line.
Now what I want is for this homepage and every other html/php page to reload every day.
Please tell me what I need to do in .htaccess to set it like that.
I have tried setting html / css to 1 day there but that is not working perfectly as my app still loads the old cache. (My app is just a browser of windowsphone 8.1, IE11 probably.)
Thanks in advance.
The browser caching will base on the resource URL.
When you want to force browser to reload the new resource, you just change the URL by adding a version string.
For example, current image URL:
http://static.adzerk.net/Advertisers/af217662e49a4cbda030feae88418cdd.png
You can change to:
http://static.adzerk.net/Advertisers/af217662e49a4cbda030feae88418cdd.png?v=1
The version number can be controlled in your PHP code.
Version control number can be read/write from a text file in case you do not use DB.

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

Full Cache Control using php

I have a website with dynamic pages.
I want to set the cache for images (all my gif, jpg and png) and css files to a specified time (1 month)
I want the browser to confirm if the files are still same on the server before serving cache copy.
How can i go about it via PHP?
We do this at the web server level, apache in our case (this question is also tagged with apache) using mod_expires.
You could do this with the following directives in your httpd.conf, or similar:
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType text/css "access plus 1 month"

Categories