Apache FilesMatch - matching a folder in the regular expression

FilesMatch should only match filenames. You can place the .htaccess file inside the skins directory and it should look something like this:

<FilesMatch "\.(jpg|png|gif)">
    ExpiresDefault A2592000
</FilesMatch>

Alternatively, in httpd.conf, you could use:

<Directory path_to_the_skins_dir>
    <FilesMatch "\.(jpg|png|gif)">
        ExpiresDefault A2592000
    </FilesMatch>
</Directory>

Good luck.


<Directory> and <Location> are disallowed in .htaccess, but…

You can use <If> which is allowed also in the “Context“ .htaccess (not just httpd.conf)… also see here. It allows you to match for a base path and an extension and anything else, a RegExp can grasp…

Tested and working:

<If "%{REQUEST_URI} =~ m#^/_stats/.*\.(jpg|png|css|js)#">
    Header unset ETag
    FileETag None

    ExpiresActive On
    ExpiresDefault "access plus 1 day"
</If>

Notes: _stats is my rewrite url, not the incoming URL (if that makes any difference on your end), not sure, why the matching works against that…

# is just a different outside “gate” to indicate a regular expression, instead of using / to mark it as a regular expression. (Since I need to use / in its literal sense, this saves me from having to escape the /'s).


Directory and Location commands don't work in .htaccess on many shared hostings. The solution could be to create .htaccess in target sub-folder, and use FilesMatch command there.