Search code examples
php.htaccessmagentocachingcdn

How to add Leverage browser caching for CDN in .htaccess?


I have below code in my .htaccess

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 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"

on

cdn.domain.com/abc.jpg (expiration not specified)
cdn.domain.com/abc.png (expiration not specified)

Same issue for JS, CSS & Other as well.

What i need to put in .htaccess so mostly achieve A Grade in that.


Solution

  • If you want to use Leverage browser caching for CDN, it's good to cache files by adding some caching headers like Cache-Control, Expires and Last-Modified.

    Leverage Browser Caching using Mod_Headers

    If you're on a shared server and your hosts won't enable Mod_Expires, you can still leverage browser caching by using Mod_headers, which will be available.

    # Leverage browser caching using mod_headers #
    <IfModule mod_headers.c>
        <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
            Header set Expires "Wed, 15 Apr 2020 20:00:00 GMT"
            Header set Cache-Control "public"
        </FilesMatch>
    </IfModule>
    # End of Leverage browser caching using mod_headers #
    

    below example for testing:

    # 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>
    

    Testing The Headers

    You can verify if the Cache-Control: max-age header is in place on your files by running a “curl” command like:

    curl -I http://foo.bar.netdna-cdn.com/file.ext
    
    HTTP/1.1 200 OK
    Date: Fri, 16 Sep 2014 14:12:20 GMT
    Content-Type: text/css
    Connection: keep-alive
    Cache-Control: max-age=604800, public  ← 1 Week caching time 
    Expires: Thu, 21 May 2015 20:00:00 GMT
    Vary: Accept-Encoding
    Last-Modified: Thu, 24 Jan 2013 20:00:00 GMT
    GMT; path=/; domain=.domain.com
    Server: NetDNA-cache/2.2
    X-Cache: HIT
    

    you have used below code:

    Browser Caching using Mod_Expires
    The most common way to leverage browser caching is to use mod_expires. The following code can be added to your .htaccess and will automatically enable browser caching for all users.

    # Leverage browser caching using mod_expires #
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType image/jpg "access plus 1 year"
        ExpiresByType image/jpeg "access plus 1 year"
        ExpiresByType image/gif "access plus 1 year"
        ExpiresByType image/png "access plus 1 year"
        ExpiresByType text/css "access plus 1 month"
        ExpiresByType application/pdf "access plus 1 month"
        ExpiresByType text/x-javascript "access plus 1 month"
        ExpiresByType application/x-shockwave-flash "access plus 1 month"
        ExpiresByType image/x-icon "access plus 1 year"
        ExpiresDefault "access plus 2 days"
    </IfModule>
    # End of Leverage browser caching using mod_expires #