To the following browser caching via mod_expires.c
in the .htaccess
...
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 day"
</IfModule>
...I would like to add an exception: One or more folders shall not be cached. I tried a version with Directory
before </IfModule>
, but that led to a 500 Internal Server Error. That
<Directory "/absolute/path/to/specialfolder">
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
</Directory>
and that snippet
<Directory "/absolute/path/to/specialfolder">
ExpiresActive Off
</Directory>
What's wrong and what could help? (for one or a few folders)
CodePudding user response:
The <Directory>
directive is not permitted in a .htaccess
context, it can only be used in a server (or virtualhost) context. (Hence the 500 error.)
The userland/.htaccess
way would be to create another .htaccess
file in that "specialfolder" with ExpiresActive Off
in order to override the parent config.
Alternatively, you could perhaps use an <If>
expression in the root .htaccess
file.
For example:
# Turn off mod_expires for a specific directory
<If "%{REQUEST_URI} =~ m#^/specialfolder/#">
ExpiresActive Off
</If>
Where REQUEST_URI
is the document-root-relative URL-path.
OR, only use mod_expires when not requesting these folder(s). For example:
# Only use mod_expires when NOT requesting the specific directories
<If "%{REQUEST_URI} !~ m#^/(specialfolder|anotherfolder)/#">
ExpiresActive On
ExpiresDefault "access plus 1 day"
</IfModule>
One or more folders shall not be cached.
Although disabling mod_expires does not necessarily prevent the resource from being cached. It simply prevents mod_expires from setting the Cache-Control
(and Expires
) headers. If you specifically want to disable caching then consider explicitly setting the relevant Cache-Control
/Expires
header(s) directly.
For example:
# Set env var when special "none-cache" folder requested:
SetEnvIf Request_URI "^/(specialfolder|anotherfolder)/" NOCACHE
# Set no-cache headers if NOCACHE env var is set
Header always set Cache-Control "no-store" env=NOCACHE
Header always set Expires "0" env=NOCACHE
In this example, you do not need to disable mod_expires since the Header
directive will override mod_expires (since mod_headers is processed after mod_expires).