.htaccessexpires-header

Setting expiry on static resources with htaccess


just got to know that setting expiry dates for static resources make your site go fast and jump in rankings.

My question is:

  1. Does browser checks create date of the static resource?

  2. If I make change to a css before 1 month, does server sends fresh copy and browser refreshes its cache?

This is the code I added to htaccess:

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>

Solution

  • From mod_expires documentation:

    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.

    Here is the syntax:

    ExpiresByType type/encoding "base[plus num type] [num type] ..."
    

    where base is one of:

    • access
    • now (equivalent to 'access')
    • modification

    The plus keyword is optional. num should be an integer value [acceptable to atoi()], and type is one of:

    • years
    • months
    • weeks
    • days
    • hours
    • minutes
    • seconds

    So if you write:

    ExpiresByType image/gif "access plus 1 month"
    

    Your file will be cached as long as visitors keep requesting it without interruption for more than a month, even if you modify it.


    You should then write instead:

    ExpiresByType image/gif "modification plus 1 month"
    

    Then your file will be cached for a month as long as it is not modified, otherwise the cache will be updated.