conditional downloading page contents (js, css) - php

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>

Related

Headers don't get set in Laravel 5.6

I'm using Laravel 5.6.
I'm trying to cache my css/js files. The way I'm doing it is using a middleware.
public function handle($request, \Closure $next, $guard = null)
{
$request->header('Cache-Control', 'max-age=86400, public');
$request->header('X-www-test-header', 'test-value');
return $next($request);
}
And wrapping basically every route in this middleware. This seems like a really bad idea from where I'm standing but to be perfectly honest I can't find another way to do it(any suggestions are welcome). But that's not the problem.
The problem is that my headers don't make thru.
I've also tried setting the headers via .htaccess
<IfModule mod_headers.c>
<filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
Header set Cache-Control "max-age=2678400, public"
</filesmatch>
<filesmatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, private, must-revalidate"
</filesmatch>
<filesmatch "\.(pdf)$">
Header set Cache-Control "max-age=86400, public"
</filesmatch>
<FilesMatch "\.(js|css)$">
ExpiresActive On
ExpiresDefault "access plus 1 weeks"
</FilesMatch>
That doesn't work either.
I'm honestly not sure what the problem is. Either laravel is somehow rewriting all the headers or apache or something.
Literally any pointing in the right direction is appreciated.
Laravel does not handle assets requests. Those requests are handled by the WEB server.
That's why try to enable apache headers module by executing a command a2enmod headers
The first problem is you are using request header getters to set a header, which won't work of course. So $request->header(key, default) will return the header with the specified key from the request and if that header is not present, the default you specified will be returned.
setting headers on response
return response($content)
->header('Content-Type', $type)
->header('X-Header-One', 'Header Value')
->header('X-Header-Two', 'Header Value');
the above code shows how you should attach header to a response and here is the Laravel documentation on that
Enable caching for static assets
So now if you want to set expiration time on assets so they can be cached, the best way to do so is to utilize your web server. For example if you are using Apache, this tutorial will help.
By the way, you should utilize your web server because for the most part, Laravel doesn't respond to static asset requests (your web server does), unless you specify so.
In short you need to enable to mod_expires module and the configure it bu setting an expiration time
a2enmod expires
and use it as belows
[...]
<IfModule mod_expires.c>
<FilesMatch "\.(jpe?g|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault "access plus 1 week"
</FilesMatch>
</IfModule>
[...]

How to set cache time specific file

I am working on a website and I've got 1 php file which I want to cache longer than the rest (1 year instead of 15 min). For the general cache settings I used a .htaccess file but is it possible to do this in htacces? Or do I have to do it in a different way?
You could put this .htaccess in the directory where there is your PHP file, but others files in this directory will be affected.
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/x-javascript "access 1 year"
ExpiresDefault "access 1 year"
</IfModule>
## EXPIRES CACHING ##
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.
Depends on what you want to do. See here for more informations.

Expires in 1 month header on all files except .php files

To speed up my website I have read about the Expires header, how to implement it in Apache, and how to only do so for certain file types. However I want to set a one month expiry on all files except .php files (webpages).
So how do I do this with .htaccess? I have a PHP script that uses filemtime to change the filename of CSS, JS files when they are edited, so that they are re-downloaded (eg if a file changes at xxxxxxx in Unix time, it changes the filename to /resource.css?recache=xxxxxxxx when it is requested). So that's taken care of. As images etc. don't change often, I want all of them to be cached for a month.
Also, what is the browser support for the Expires header?
Use the mod_expires in your Apache configuration files. For example:
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
# Set up caching on media files for 1 month
<filesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|gif|jpg|jpeg|png|swf)$">
ExpiresDefault A2592000
Header append Cache-Control "public"
</filesMatch>
You can read more here

Cache for images, php, js, and html

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>

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

Categories