wordpressapache.htaccesscachingmod-expires

Exclude URLs from "Expires"-Headers via .htaccess


ExpiresActive On
ExpiresDefault "access plus 1 day"
 [...]
RewriteRule .* index.php [L]

This are my settings for mod_expires plus the relevant RewriteRule. They work perfectly! But I have a specific URL, that shall not be cached. The regex for that would be ^/?nocache/. Now that directory (/nocache/) does not even physically exist; requests to it - as to every other directory - are handled by the "index.php".

My first thought was to add a <LocationMatch> around it, but the documentation sais, it is not allowed in the ".htaccess" context. A <FilesMatch> would be allowed but needs the file to physically exist, I think ((true?)).

Any ideas?


Solution

  • You can use RewriteRule directive in .htaccess:

    ExpiresActive On
    ExpiresDefault "access plus 1 day"
     [...]
    
    RewriteEngine On
    RewriteRule ^nocache - [E=NOCACHE:1]
    
    SetEnvIf REDIRECT_NOCACHE 1 NOCACHE=1
    
    # DO NOT cache /nocache/ URIs
    Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform" env=NOCACHE
    Header set Pragma "no-cache" env=NOCACHE