Bug 62418 - Be able to apply directives to a single file
Summary: Be able to apply directives to a single file
Status: NEW
Alias: None
Product: Apache httpd-2
Classification: Unclassified
Component: Core (show other bugs)
Version: 2.5-HEAD
Hardware: PC Linux
: P2 enhancement (vote)
Target Milestone: ---
Assignee: Apache HTTPD Bugs Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-05-30 13:47 UTC by HONTVÁRI Levente
Modified: 2018-05-30 13:47 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description HONTVÁRI Levente 2018-05-30 13:47:52 UTC
I had to apply an ExpiresDefault directive to a single file. This is surprisingly difficult and differ between Apache versions. I have come up with two solutions, see https://serverfault.com/questions/914295/how-to-apply-an-expiresdefault-directive-to-a-single-file-excluding-similar-fil/914443#914443

First solution:

<If "%{REQUEST_FILENAME} == '%{DOCUMENT_ROOT}/index.html'">
    ExpiresDefault "access plus 1 hours"
</If>

This works in a .htaccess file, but it is awkward, because the right side of the condition must include the path to the .htaccess file. The directory should be implicit from the location of the .htaccess. This example is the best case, in a  wronger case the expression would be something linke %{DOCUMENT_ROOT}/alfa/beta/index.html, if the .htaccess file is in alfa/beta.

The second solution:
<DirectoryMatch "^/srv/www-example-com/[^/]*$">
    <Files "index.html">
        ExpiresDefault "access plus 1 hours"
    </Files>
</DirectoryMatch>

This does not work in .htaccess. It is also very verbose. 

It also shows a bug in this Apache HTTPD version (2.4.18): DirectoryMatch matches the full path of URL, including the last file name part. This is why the closing /[^/]* expression is required before the $ sign. Otherwise it would not match the www.example.com/index.html URL.

What I would expect is something like:

<File file1 file2 file3>
    ExpiresDefault "access plus 1 hours"
</File>

which does not match files in subdirectories, only the current directory in .htaccess. it works for multiple files, and it is short. If used in a virtual host context, then file1 must include the absolute path.