0

For all files, except pdf, in a specific directory as well as its sub-directories on the server, I would like to set the expiration header to 10 hours. How can I do this in the .htaccess file?

<Directory "/foldername">
   <IfModule mod_expires.c>
      ExpiresActive On
      ExpiresByType * "access plus 10 hours"
   </IfModule>
</Directory>

I understand Directory cannot be used in .htaccess. But how to do this?

4
  • Put the .htaccess into the "specific directory", and do it in there without any further restriction? Commented Oct 12, 2022 at 13:15
  • Or wrap it into an If instead, that checks that the request URI starts with your folder name. httpd.apache.org/docs/2.4/expr.html#examples Commented Oct 12, 2022 at 13:17
  • Thanks @CBroe, so if I put the .htaccess file in the folder, it will automatically apply to its subfolders? And is ExpiresByType * correct, i.e., is the * allowed? Commented Oct 12, 2022 at 13:21
  • Yes, it will automatically apply to subfolders. ExpiresByType * won't work though, according to documentation the first argument needs to be a mime type. But ExpiresDefault also exists. Commented Oct 12, 2022 at 13:23

1 Answer 1

1

You could put the .htaccess into the specific directory, and do it in there without any further restriction. Or use an If condition, to apply this based on what the request URI starts with.

ExpiresByType * won't work, according to the documentation, this needs an actual mime type as "argument".

But ExpiresDefault also exists, and allows you to specify the default expiry for all files.

Sign up to request clarification or add additional context in comments.

5 Comments

At a folder higher, there's also a .htaccess file that sets the expiration data per mime type, including a default. So I essentially need to overwrite those settings for this specific folder. To this end, I've also specified the expiration per mime type in the htaccess file in the specific folder. It works for the mime types not specified a level higher up, but for those that are now in both htaccess files, the configuration from the htaccess at a folder higher up, seems to take precedent. How to change this?
Those you will have to override explicitly then. The default applies only for those types, that did not have any specific value configured yet.
In the .htaccess in the specific folder, I have: <IfModule mod_expires.c> ExpiresActive On ExpiresByType text/html "access plus 6 hours" ExpiresByType text/css "access plus 6 hours" ExpiresByType application/javascript "access plus 6 hours" ExpiresByType application/x-javascript "access plus 6 hours" ExpiresByType text/javascript "access plus 6 hours" ExpiresByType application/xhtml-xml "access plus 6 hours" </IfModule> But still the expiration header for css and js files (in a subfolder of the specific folder) is equal to that set in the htaccess at the higher level.
"(in a subfolder of the specific folder)" - and directly in the folder itself? Since these settings should "inherit" further down, this rather sounds like your configuration did not properly apply to begin with.
I looked and also directly in the specific folder itself, it's still using the setting of the htaccess that exists a folder up, instead of the one in the specific folder. But this must be a separate issue, if you have any suggestions I very much welcome them, but I'll already accept the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.