Understanding htaccess cache rules – Webmasters Stack Exchange

Ok.

I’ve read a lot and I understand almost nothing.

I talk about cache, and I don’t know any cache system that is interested in keeping my files, for me, and for free. And, as I read, there are almost 5 other cache systems triggered when a user/client access let’s say www.webmasters.stackexchange.com

Ok, I know one cache. My visitor browser’s cache, which, once he accesses my website, he (the browser) may save the resources into his system (where the browser set it). The next time when the user navigates to the same page, the resources are loaded from his system and not from my website.

Further, I want to take advantage of the most important cache methods for my website. So I use some rules. The problem is that it doesn’t help me just to have them. I also want to understand them.

The code I found from stack overflow and other sites is (which I will modify further):

# Block 1 #
<ifModule mod_expires.c>
    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 text/html "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access plus 2 months"
    Header set Cache-Control "max-age=290304000, public"
</ifModule>

# Block 2 #
<IfModule mod_headers.c>
    <FilesMatch ".(js|css|xml|svg|gz)$">
        Header append Vary Accept-Encoding
    </FilesMatch>
</IfModule>

# Block 3 #
<IfModule mod_deflate.c>
    <filesMatch ".(js|css|html|php|txt|svg|otf|ttf|woff2|xml)$">
        SetOutputFilter DEFLATE
    </filesMatch>
</IfModule>

In the first block, I have some instructions but ONLY if mod_expires module is available. Anyway, inside the block 2, I also have at the last line:

    Header set Cache-Control "max-age=290304000, public"

Is it true that this line doesn’t belong to this block as instructions will be loaded only if mod_expires is available ? If mod_headers is disabled, then I will probably get a 500 error. So this line should better be in the <IfModule mod_headers.c> block. Isn’t it ?

So, the code looks now like this:

# Block 1 #
<ifModule mod_expires.c>
    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 text/html "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access plus 2 months"
</ifModule>

# Block 2 #
<IfModule mod_headers.c>
    <FilesMatch ".(js|css|xml|svg|gz)$">
        Header append Vary Accept-Encoding
    </FilesMatch>
    
    # Line from block 1 #
    Header set Cache-Control "max-age=290304000, public"
</IfModule>

# Block 3 #
<IfModule mod_deflate.c>
    <filesMatch ".(js|css|html|php|txt|svg|otf|ttf|woff2|xml)$">
        SetOutputFilter DEFLATE
    </filesMatch>
</IfModule>

I’m not interested in checking if module xyz is available. It must be. So I will remove IF conditions. If, for some reason, one module won’t be available in the future, then I’ll probably get a 500 error, which is fine for me. I want to take advantage of the cache methods for true, not if whatever. It’s something wrong here ?

So, the code looks now like this:

# Block 1 #
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 text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access plus 2 months"

# Block 2 #
Header set Cache-Control "max-age=290304000, public"
<FilesMatch ".(js|css|xml|svg|gz)$">
    Header append Vary Accept-Encoding
</FilesMatch>

# Block 3 #
<filesMatch ".(js|css|html|php|txt|svg|otf|ttf|woff2|xml)$">
    SetOutputFilter DEFLATE
</filesMatch>

Inside block 1 I have the first line which sets “ExpiresActive on”, which is clear. I activate the module even if it is available.

Next, on second line I set ExpiresByType image/jpg “access 1 year”

Is this the cache system for the user’s browser (I talked above) ? I understand well ?

What cache availability should I set here ? It depends on the file ? I see 1 year for some files and one month for others. What should I consider when set the expiration time ?

Why:

ExpiresByType image/jpg "access 1 year"

And not:

ExpiresByType image/jpg "1 year"

What’s access keyword doing here ?

A user access my website which contains one jpg image. Next, the browser loads and save that image and keeps it for one year. After one year, the browser automatically delete this image and the next time the user access my website, the browser will save the jpg image again for another year. If the user cleans his browser cache after let’s say 2 months, the next time when the user access my website, the browser saves the image again, even if a year hasn’t passed. Do I understand ok ?

What types of files should I put here ?

I know that images, css files and javascript files are almost required. Do I need to put other files here ?

Does ExpiresDefault rule works for php files too ? I mean, if I have a contact.php file with some content and I modify the content of contact.php, because of ExpiresDefault rule, the content won’t change to the user ?

If one image file was changed, how would I force the browser to re-download the file ? I understand that the recommendation would be to set another link for that image, but let’s say I want to use the same url. The name of that image may be good for optimization and I may not want to change his name or even his directory. So, if the image was already saved to the user, how can I force a re-download ?

Now, last line of block 1 is:

ExpiresDefault "access plus 2 months"

I understand that this is the default expiration time, but for what type of files ?

In the block 2 I have now:

Header set Cache-Control "max-age=290304000, public"

Does this has something to do with block 1 ? If the answer is yes, then means that a file saved in the user’s browser won’t exceed the time set in max-age even if I set ExpiresByType image/jpg "access 999 years" ?

What files are included in Header set Cache-Control "max-age=290304000, public" ? For what types of files is this max-age cache available ?

Can someone give me an example of what are the benefits of Header append Vary Accept-Encoding rule ? And an example of what happens if I don’t use it ?

In the block 2, the Header append Vary Accept-Encoding is available only for js, css, xml, svg, gz files. There are other files should I put here ? What should I consider when I set those file types ?

Can someone explain what does SetOutputFilter DEFLATE with an example ? And what happens if I don’t use this ?

Here, almost like in block 2, the line SetOutputFilter DEFLATE is available for js, css, html, php, txt, svg, otf, ttf, woff2 and xml files. Should the php files be here ? Should I put other file types here too ?


That’s all. Please, be kind and don’t condemn my questions. I just try to understand what I’m doing…