0

I have created a website and the speed of my website is very slow. i want to enable browser cache for my website how can i do this ?

1

Easiest way would be to use a CDN like Cloudflare. They will automatically cache your static resources, and serve them over HTTP2 as well if you want. This should have an immediate effect on the speed of your site.

| improve this answer | |
  • 2
    Would this cache the resources on the client side or does it cache on cloudflare servers? – ma77c Apr 10 '17 at 13:53
  • Both, depending on Cloudflare settings. – Tom Gullen Apr 10 '17 at 15:38
1

When you connect to your FTP server, create .htaccess file and paste one of these codes in it:

Adding Expire Headers

ExpiresActive On
ExpiresDefault A0

# 1 YEAR - doesn't change often
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A31536000
</FilesMatch>

# 1 WEEK - possible to be changed, unlikely
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
ExpiresDefault A604800
</FilesMatch>

# 3 HOUR - core content, changes quickly
<FilesMatch "\.(txt|xml|js|css)$">
ExpiresDefault A10800
</FilesMatch>

Using max-age headers

# 1 YEAR
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>

# 1 WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

# 3 HOUR
<FilesMatch "\.(txt|xml|js|css)$">
Header set Cache-Control "max-age=10800"
</FilesMatch>

# NEVER CACHE - notice the extra directives
<FilesMatch "\.(html|htm|php|cgi|pl)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
</FilesMatch>

Both of these types cache files with the extension that is in parentheses "()". You can change these file extensions if you feel some files are going to be edited more frequently. Don't forget to split them with the vertical bar "|"

You can check if your caching works on Redbot.org

More about HTTP caching here

| improve this answer | |
  • 1
    Just to clarify, both these methods set Cache-Control max-age headers, it's just that using mod_expires (the first example) also sets the Expires header for backwards compatibility. (The max-age header takes priority in all modern browsers.) – DocRoot Apr 10 '17 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.