• Resolved Anonymous User 14978628

    (@anonymized-14978628)


    Hi, this looks like a great plugin. I wondering if you could clarify a few things for me?

    1) This plugin tells my CDN how long to cache a page for? Is that correct? So if i have a page that never changes once published, i could set a long cache time such as 1 month?

    2) What do you mean by the following at the bottom of your plugin options:

    “You should also set Cache-Control headers manually in your webserver configuration for images, scripts, stylesheets, and other static resources.”

    So if i create a page and it is cached by a caching plugin (e.g., WP Super Cache) will your plugin then not apply to all images, css, JS on that cached page?

    Currently i have browser caching enabled in htaccess as follows:

    ## EXPIRES CACHING ##
    <IfModule mod_expires.c>
    # Add correct content-type for fonts
    AddType application/vnd.ms-fontobject .eot 
    AddType application/x-font-ttf .ttf
    AddType application/x-font-opentype .otf
    AddType application/x-font-woff .woff
    AddType image/svg+xml .svg
    ExpiresActive On
    ExpiresDefault "access 1 month"
    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 image/webp "access 1 year"
    ExpiresByType image/ico "access 1 year"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType text/javascript "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType text/html "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType application/x-javascript "access 1 month"
    ExpiresByType application/javascript "access 1 month"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
    ExpiresByType application/x-font-ttf "access plus 1 year"
    ExpiresByType application/x-font-opentype "access plus 1 year"
    ExpiresByType application/x-font-woff "access plus 1 year"
    ExpiresByType application/font-woff2 "access 1 month"
    ExpiresByType image/svg+xml "access plus 1 year"
    </IfModule>
    ## EXPIRES CACHING ##
    <IfModule mod_headers.c>
    Header unset Pragma
    Header unset ETag
    FileETag None
    Header set Connection keep-alive
    </IfModule>

    To set cache control header for images would i then need to add the following to my htaccess? Or does your plugin already take care of this if a page is cached by something like WP Super Cache?

    # 1 MONTHS
    <FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=2419200, public"
    ExpiresDefault "access plus 1 month"
    SetOutputFilter DEFLATE
    </FilesMatch>

    Or is there no need to set the max-age directive with the cache-control header since it is already set by the mod_expires module for expires caching? As such:

    # BEGIN Cache-Control Headers
    <ifModule mod_headers.c>
        <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
            Header set Cache-Control "public"
        </filesMatch>
        <filesMatch "\.(css)$">
            Header set Cache-Control "public"
        </filesMatch>
        <filesMatch "\.(js)$">
            Header set Cache-Control "public"
        </filesMatch>
        <filesMatch "\.(x?html?|php)$">
            Header set Cache-Control "public"
        </filesMatch>
    </ifModule>
    # END Cache-Control Headers
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Dan

    (@geekysoft)

    1) Correct.

    2A) No, the Cache-Control plugin only works on dynamic files generated by WordPress. The caching policies and headers for your static files, such as your robots.txt or any uploaded images, are controlled by your HTTP server.

    2B) The Cache-Control plugin won’t work with WP Super Cache. WP Super Cache disregards caching headers and apply its own cache policies. WP Super Cache works as a dub HTTP-unaware local-caching proxy. The Cache-Control plugin is meant to work with HTTP-aware caches (such as your web browser, or a caching proxy like Varnish, Apache mod_proxy, or a CDN).

    2C) You can send both the Expires header (mod_expires) and a Cache-Control header at the same time. Expires contain an exact date in the future when the cache will expire, where as a Cache-Control: max-age=seconds header instructs a client (or intermediary cache) how many seconds they should use the cached version before fetching a new copy. Expires is a left-over from HTTP/1.0. Most implementations prefer Cache-Control headers and ignore Expires headers.

    • This reply was modified 7 years, 8 months ago by Dan.
    Thread Starter Anonymous User 14978628

    (@anonymized-14978628)

    Thanks for the explanation. I wanted to use this plugin because i noticed my http headers were outputting the following:

    Cache-Control: no-cache, must-revalidate, max-age=0

    So this seemed to tell me that the page was saying not to be cached, but please correct me if my understanding is incorrect.

    So after enabling your plugin i get the following for the homepage which i set for 1 day:

    Cache-Control: max-age=86400

    and for posts which i set for 1 month:

    Cache-Control: max-age=2592000

    So currently i am using Comet Cache and KeyCDN. My intention is to have KeyCDN cache the homepage for 1 day, and posts for 1 month.

    I have also added the following to my htaccess to cache static resources as you recommend in the plugin:

    # BEGIN Cache-Control Headers
    <ifModule mod_headers.c>
    <filesMatch “\.(ico|jpg|jpeg|png|gif|webp|swf)$”>
    Header set Cache-Control “max-age=31536000, public”
    </filesMatch>
    <filesMatch “\.(css)$”>
    Header set Cache-Control “max-age=2592000, public”
    </filesMatch>
    <filesMatch “\.(js)$”>
    Header set Cache-Control “max-age=2592000, public”
    </filesMatch>
    <filesMatch “\.(woff|woff2|ttf|svg|eot|otf)$”>
    Header set Cache-Control “max-age=31536000, public”
    </filesMatch>
    </ifModule>
    # END Cache-Control Headers

    So this would make the KeyCDN cache for images 1 year, css 1 month, js 1 month, and fonts 1 year.

    Could you tell me if this is all now setup correctly for what i wanted, whereby i wanted to specify how long a post, the homepage, and static resources are cached for on KeyCDN?

    EDIT

    I just noticed after testing my site with pingdom when i look at the response headers for an image file i get the following:

    Cache-Control no-cache

    but the correct value is set for the request header

    Cache-Control max-age=2592000

    Plugin Author Dan

    (@geekysoft)

    Cache-Control: no-cache, must-revalidate, max-age=0

    So this seemed to tell me that the page was saying not to be cached, but please correct me if my understanding is incorrect.

    That is exactly what it’s saying.

    So after enabling your plugin i get the following for the homepage which i set for 1 day: Cache-Control: max-age=86400

    and for posts which i set for 1 month: Cache-Control: max-age=2592000

    So currently i am using Comet Cache and KeyCDN. My intention is to have KeyCDN cache the homepage for 1 day, and posts for 1 month.

    That seems correct. I’m not familiar with KeyCDN, specifically. At some CDNs you have to enable an option called something like “Respect original caching policy/headers”. At most CDN providers, this is the default behavior.

    Header set Cache-Control “max-age=31536000, public

    public is the default value and you don’t need to set it. It’s so rarely useful to explicitly set it that you’re unlikely to ever have to set it manually.

    <filesMatch “\.(ico|jpg|jpeg|png|gif|webp|swf)$”>

    You should use LocationMatch. It’s faster than FilesMatch because the matching is done on the Location (meaning the incoming request URL) instead of having to do the matching after it has found the corresponding file on your disk.

    Otherwise it seems like your setup should do what you want.

    I just noticed after testing my site with pingdom when i look at the response headers for an image file i get the following:

    Use https://redbot.org/ to test your headers.

    Thread Starter Anonymous User 14978628

    (@anonymized-14978628)

    I tried using location match but it gives me “500 internal server error”. Is this the correct way to do it?

    # BEGIN Cache-Control Headers
    <ifModule mod_headers.c>
        <locationMatch "\.(ico|jpg|jpeg|png|gif|webp|swf)$">
            Header set Cache-Control "max-age=31536000"
        </locationMatch>
        <locationMatch "\.(css)$">
            Header set Cache-Control "max-age=2592000"
        </locationMatch>
        <locationMatch "\.(js)$">
            Header set Cache-Control "max-age=2592000"
        </locationMatch>
        <locationMatch "\.(woff|woff2|ttf|svg|eot|otf)$">
            Header set Cache-Control "max-age=31536000"
        </locationMatch>
    </ifModule>
    # END Cache-Control Headers

    EDIT

    ok i realized locationmatch is expressed differently to filesmatch. How would i implement this correctly for the wp-contents directory and subfolders. Would it be something like this:

    <LocationMatch “^/wp-content”>
    Header set Cache-Control “max-age=31536000”
    </LocationMatch>

    But doesn’t that then apply to everything in the wp-contents folder? How would i specify particular filetypes (e.g., image vs css files) to give them a unique max age?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Image Caching’ is closed to new replies.