How to enable cached js, css, images

Status
Not open for further replies.

onplayer

New Member
Messages
11
Reaction score
0
Points
1
Hello!

I have a problem, please help.

PageSpeed Insights it says that the files are not cached in the browser.

Page_Speed_Insights.png


I have tried different ways in .htaccess, nothing turns out.

My .htaccess:

Code:
# Remove index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

# Redirect www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Enabled GZIP
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/javascript
    <IfModule mod_setenvif.c>
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    </IfModule>
</IfModule>


What do I need to write in .htaccess in order to enable caching js, css, images files?
 

caftpx10

Well-Known Member
Messages
1,534
Reaction score
114
Points
63
Note that you only enabled gzip via your htaccess file which compresses and not caches the files.

If you're talking about it most of the time being cached (when being reloaded) then the best approach would be to use something like a CDN. If you want the normal load-from-cache-when-either-going-back-a-page-or-on-browser-launch then the htaccess caching method should do the job. Note that CDNs can do the same thing.
 

onplayer

New Member
Messages
11
Reaction score
0
Points
1
Note that you only enabled gzip via your htaccess file which compresses and not caches the files.

If you're talking about it most of the time being cached (when being reloaded) then the best approach would be to use something like a CDN. If you want the normal load-from-cache-when-either-going-back-a-page-or-on-browser-launch then the htaccess caching method should do the job. Note that CDNs can do the same thing.


I deleted gzip (from .htaccess). Now my .htaccess:
Code:
# Remove index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

# Redirect www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


But the files still will not be cached.

I not can use CDN.

caftpx10, please help! what need add in .htaccess for cache files?
 

Dead-i

x10Hosting Support Ninja
Community Support
Messages
6,084
Reaction score
368
Points
83
Hi,

The Google page is actually reporting that browsers are being instructed not to cache your static files. This is because of the line at the very bottom of your .htaccess file (at /public_html/.htaccess) to disable the Varnish cache:
Header set Cache-Control "max-age=0, private, no-cache, no-store, must-revalidate"

This line sends the "Cache-Control" header for every file that is served from your site. Varnish interprets this, and disables server-side caching when it sees the Cache-Control header. However, browsers interpret this line too, and browsers won't cache files served with this header, which is what Google PageSpeed is picking up on.

A solution for this might be to change your .htaccess file so that only dynamic files (PHP files) get sent the Cache-Control header, and static files (images, CSS and JavaScript) don't get sent this header. This would mean that Varnish and your browsers would cache images and styles, but not dynamic files. You can filter what files get sent the Cache-Control header using the <Files> tag.

Thank you,
 
Status
Not open for further replies.
Top