skip to Main Content

Reason, why i need disable cache:

When i create post or page. It caches its. If i create second post or page, then it updates old post or page.
When i disabled cache for each pages, this bug was fixed. Although i need cache for SEO.

So, i need disable cache only in admin pages.

.htaccess

<ifModule mod_headers.c>
  <filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|swf|woff2)$">
  Header set Cache-Control "max-age=2592000, public"
  </filesMatch>
  <filesMatch ".(css|js)$">
  Header set Cache-Control "max-age=86400, public"
  </filesMatch>
  <filesMatch ".(xml|txt)$">
  Header set Cache-Control "max-age=172800, public, must-revalidate"
  </filesMatch>
  <filesMatch ".(html|htm|php)$">
  Header set Cache-Control "max-age=1800, private, must-revalidate"
  </filesMatch>

  <FilesMatch ".(js|css|jpg|png|jpeg|gif|xml|json|txt|pdf|mov|avi|otf|woff|ico|swf)$">
  RequestHeader unset Cookie
  Header unset Cookie
  Header unset Set-Cookie
  </FilesMatch>


  # Guarantee HTTPS for 1 Year including Sub Domains
  Header always set Strict-Transport-Security "max-age=31536000;"
</ifModule>

function.php

if (is_admin()) {
      header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
      header('Cache-Control: no-store, no-cache, must-revalidate');
      header('Cache-Control: post-check=0, pre-check=0', FALSE);
      header('Pragma: no-cache');
}

My code in function.php don’t working.

3

Answers


  1. Remove this line from the .htacess

    Header set Cache-Control “max-age=86400, public”
    
    Login or Signup to reply.
  2. I’m afraid that any cache headers set in .htaccess have priority, and it is impossible to override them in PHP.

    The only solution that I can think of is to disable the caching setup in the .htaccess and manually set the headers in each file. It’s a pain, I know, but it’s all that I can fathom.

    The below answer will set caching to the browser default, which I assume is not what you intend.

    Login or Signup to reply.
  3. Remove these line from the .htacess

    <filesMatch ".(html|htm|php)$">
      Header set Cache-Control "max-age=1800, private, must-revalidate"
    </filesMatch>
    

    You just need lines like below

    <IfModule mod_expires.c>
    AddType application/font-woff2 .woff2
    AddType application/x-font-opentype .otf
    ExpiresActive On
    ExpiresDefault A0
    ExpiresByType video/webm A10368000
    ExpiresByType video/ogg A10368000
    ExpiresByType video/mp4 A10368000
    ExpiresByType image/webp A10368000
    ExpiresByType image/gif A10368000
    ExpiresByType image/png A10368000
    ExpiresByType image/jpg A10368000
    ExpiresByType image/jpeg A10368000
    ExpiresByType image/ico A10368000
    ExpiresByType image/svg+xml A10368000
    ExpiresByType text/css A10368000
    ExpiresByType text/javascript A10368000
    ExpiresByType application/javascript A10368000
    ExpiresByType application/x-javascript A10368000
    ExpiresByType application/font-woff2 A10368000
    ExpiresByType application/x-font-opentype A10368000
    ExpiresByType application/x-font-truetype A10368000
    </IfModule>
    ## EXPIRES CACHING ##
    <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    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>
    
    <ifModule mod_headers.c>
      <filesMatch ".(jpg|jpeg|png|gif|ico|svg|css|js)$">
    Header set Cache-Control "max-age=31536000, public"
      </filesMatch>
    </ifModule>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search