1

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>

Question 1:

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>

Question 2:

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>

Question 3 (about Block 1):

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 ?

Question 4 (about Block 1):

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 ?

Question 5 (about Block 1):

Why:

ExpiresByType image/jpg "access 1 year"

And not:

ExpiresByType image/jpg "1 year"

What's access keyword doing here ?

Question 6 (about Block 1):

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 ?

Question 7 (about Block 1):

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 ?

Question 8 (about Block 1):

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 ?

Question 9 (about Block 1):

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 ?

Question 10 (about Block 1):

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 ?

Question 11 (about Block 2):

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" ?

Question 12 (about Block 2):

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

Question 13 (about Block 2):

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 ?

Question 14 (about Block 2):

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 ?

Question 15 (about Block 3):

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

Question 16 (about Block 3):

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

5
  • Dang, can you reopen please - I was half-way through writing an answer! Whilst it looks like a lot of questions, they are all very closely linked and in fact several of the "questions" can be reduced into one. And several are simple "yes" / "no" answers. – MrWhite Jan 12 at 12:46
  • @MrWhite All questions are based on what I don't understand and someone close it because are too many questions. I don't know what to edit to receive the same answers. Removing questions removes answers. After all, should I open 16 subjects ? There are all closest to the same code and same subject, cache... – Andrei Jan 12 at 14:00
  • Andrei, if you post as different questions, this would be better. The idea behind this site is users can search for specific questions and get specific answers. Since you have so many questions, a user looking to solve a specific question, would have to read a lot of content that would be better broken out. HTH. – Trebor Jan 12 at 15:53
  • 1
    I think you should do more research on what mod_expires (ExpiresByType / ExpiresDefault) and Header set Cache-Control "max-age" actually do (by all means ask questions if you get stuck). You should then realise that these directives are in fact mutually exclusive - which should help answer several of your questions above. Also look at the mime-types your server is responding with. Are you really serving Flash content? – MrWhite Jan 12 at 20:22
  • 1
    Your last 4 questions (#13 to #16) could perhaps be split into their own question as these aren't really related to caching (first part). (Ok, the Vary header is cache related - but the reason for setting this in the first place is to do with compression, ie. SetOutputFilter DEFLATE) – MrWhite Jan 12 at 20:33