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>
[...]
Related
I am having issues with server variables (like the page URL) being cached so that when you go to a new page, the variable that contains the server URL doesn't actually change.
I am able to fix it in my .htaccess file with this code:
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
<FilesMatch "\.(css|flv|gif|htm|html|ico|jpe|jpeg|jpg|js|mp3|mp4|png|pdf|swf|txt)$">
<IfModule mod_expires.c>
ExpiresActive Off
</IfModule>
<IfModule mod_headers.c>
FileETag None
Header unset ETag
Header unset Pragma
Header unset Cache-Control
Header unset Last-Modified
Header set Pragma "no-cache"
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
</IfModule>
</FilesMatch>
So, I know the fix, but I need to know what is causing it because it's a WordPress site (and no, it's not being caused by plugins, I tested with a fresh WP install) and therefore my .htaccess file gets periodically overwritten by WordPress.
So I would like to know how to fix it on the server side, rather than using the .htaccess file. Would it be in the PHP variables? Or apache settings?
I called the webhost (godaddy) and the guy had no idea.
Thanks!
WordPress 5.6 added application passwords.
It wants to add this to .htaccess:
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
I would try:
chmod 600 .htaccess
add_filter('wp_is_application_passwords_available', '__return_false');
Best wishes,
Mitchell
I have a website that uses a number of simple PHP include commands to call up scripts on multiple pages. The most simple example is the shared navigation menu that can be dynamically updated as needed and called upon with the following line of code:
<?php include("nav.php"); ?>
This has worked without issue for several years. However, our webhost (Arvixe) has just moved us to a new server and suddenly the includes have stopped working altogether. First thing I did to try to resolve the issue was upload the .htaccess file from the original server, but that has had no effect. I've been working with their support team for almost a week now, but they can't figure out what's going on and I'm not sure how to better communicate what is needed, at this point.
Here's the .htaccess file content:
AddHandler server-parsed .html
AddHandler application/x-httpd-php .html
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "max-age=0, public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "max-age=0, public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "max-age=0, private"
</filesMatch>
</ifModule>
# END Cache-Control Headers
Header set Set-Cookie "VC-NoCache=1; max-age=900; path=/"
Header set VC-NoCache "1"
Any idea what might have changed on their end that needs to be rectified? Thanks.
I am looking for a way to implement cache control & expire headers to my images/css/js files. I looked for tutorials & only found ways to add cache control using file extensions. The code below is the one got from http://betterexplained.com/
<FilesMatch "\.(jpg|jpeg|png|gif)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
This sets all the images of the mentioned extensions to be cached for one week. However I don't want all the images to be cached, because there are some images that are upload by the users. I only want my static images eg: logo, icons used for buttons to be cached. It would be great if someone could point out a way or a tutorial on how this could be achieved.
or again you could just move the htaccess file in the folder that will be cached and save the user image in another :D
You can change the FilesMatch filter to include a part of a path. I assume the user images are in another directory than your static images.
i.e.
<FilesMatch "^/staticdir/.+\.(jpg|jpeg|png|gif)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
You can make your regex more restricitive by including only only know images for caching:
<FilesMatch "(logo|icon|favicon|header)\.(ico|jpe?g|png|gif)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
see I am trying to do some changes in my php, smarty web site. But changes are not reflecting .
this is the .htaccess code . Is there any errror?
# Begin cache control #
ExpiresActive on
<FilesMatch ".*">
Header unset Cache-Control
Header unset Expires
Header unset Last-Modified
FileETag None
Header unset Pragma
</FilesMatch>
# End cache control #
isn't it .htaccess instead of .hntaccess? Sorry but I stall can't make comments!
I am trying to implement GZIP compression for my website. I copied the below code in my .htaccess file:
ExpiresActive On
ExpiresDefault A604800
Header append Cache-Control "public"
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
what happens is when I type username and password the page reloads but still the login form is displayed but session is set. When I refresh the page using ctrl + R the login form goes and the username is displayed. what will be the problem. wwaiting for ur reply.
I think it's mod_expires settings, not mod_gzip cause such behavior.
You just told your browser not to reload your page for the the week. It obeys.
I think you have to put expires settings at least inside of FilesMatch container. Or take any other sensible actions according to your real needs.
I guess, its just because of your browser cache, try clearing your browser cache. Or give it a try on different browser to confirm it is a cache issue or not.