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?

Nick
  • 3,496
  • 7
  • 42
  • 96
  • Put the .htaccess _into_ the "specific directory", and do it in there without any further restriction? – CBroe Oct 12 '22 at 13:15
  • Or wrap it into an `If` instead, that checks that the request URI starts with your folder name. https://httpd.apache.org/docs/2.4/expr.html#examples – CBroe Oct 12 '22 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? – Nick Oct 12 '22 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`](https://httpd.apache.org/docs/2.4/mod/mod_expires.html#expiresdefault) also exists. – CBroe Oct 12 '22 at 13:23

1 Answers1

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.

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • 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? – Nick Oct 12 '22 at 13:53
  • Those you will have to override explicitly then. The default applies only for those types, that did not have any specific value configured yet. – CBroe Oct 12 '22 at 13:59
  • In the `.htaccess` in the specific folder, I have: ` 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" ` 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. – Nick Oct 12 '22 at 14:05
  • _"(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. – CBroe Oct 12 '22 at 14:07
  • 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. – Nick Oct 12 '22 at 14:13